Module: VagrantGitHooks::UiHelpers
- Defined in:
- lib/vagrant-git-hooks/helpers.rb
Defined Under Namespace
Classes: MissingTranslationError, UnsupportedLocaleError
Constant Summary
collapse
- SUPPORTED =
%i[en fr].freeze
- OUR_NAMESPACES =
%w[messages. errors. usage. help. log. emoji.].freeze
- EMOJI =
{
success: "โ
",
info: "๐",
ongoing: "๐",
warning: "โ ๏ธ",
error: "โ",
version: "๐พ",
broom: "๐งน",
question: "โ",
bug: "๐"
}.freeze
- NS =
"vgh"
Class Method Summary
collapse
Class Method Details
.debug(ui, msg) ⇒ Object
112
113
114
115
116
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 112
def debug(ui, msg)
return unless debug_enabled?
say(ui, "#{e(:bug)} #{msg}")
end
|
.debug_enabled? ⇒ Boolean
108
109
110
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 108
def debug_enabled?
ENV["VGH_DEBUG"].to_s == "1"
end
|
.e(key, no_emoji: false) ⇒ Object
90
91
92
93
94
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 90
def e(key, no_emoji: false)
return "" if no_emoji || ENV["VGH_NO_EMOJI"] == "1"
EMOJI[key] || ""
end
|
.error(ui, msg) ⇒ Object
104
105
106
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 104
def error(ui, msg)
ui&.error(msg) || warn(ui, msg)
end
|
.namespaced(key) ⇒ Object
70
71
72
73
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 70
def namespaced(key)
k = key.to_s
k.start_with?("#{NS}.") ? k : "#{NS}.#{k}"
end
|
.our_key?(key) ⇒ Boolean
86
87
88
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 86
def our_key?(key)
OUR_NAMESPACES.any? { |ns| key.to_s.start_with?(ns) }
end
|
.print_general_help ⇒ Object
118
119
120
121
122
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 118
def print_general_help
setup_i18n!
puts t("help.general_title")
t_hash("help.commands").each_value { |line| puts " #{line}" }
end
|
.print_topic_help(topic) ⇒ Object
124
125
126
127
128
129
130
131
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 124
def print_topic_help(topic)
setup_i18n!
topic = topic.to_s.downcase.strip
return print_general_help if topic.empty?
body = t("help.topic.#{topic}", default: nil)
body ? puts(body) : print_general_help
end
|
.say(ui, msg) ⇒ Object
96
97
98
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 96
def say(ui, msg)
ui&.info(msg) || puts(msg)
end
|
.set_locale!(lang) ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 48
def set_locale!(lang)
setup_i18n!
sym = lang.to_s[0, 2].downcase.to_sym
unless SUPPORTED.include?(sym)
raise UnsupportedLocaleError,
"#{EMOJI[:error]} Unsupported language: #{sym}. Available: #{SUPPORTED.join(", ")}"
end
::I18n.locale = sym
::I18n.backend.load_translations
end
|
.setup_i18n! ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 28
def setup_i18n!
return if defined?(@i18n_setup) && @i18n_setup
::I18n.enforce_available_locales = false
base = File.expand_path("../../locales", __dir__)
paths = Dir[File.join(base, "*.yml")]
if paths.empty? && File.directory?(base)
paths = Dir.children(base).grep(/\.ya?ml\z/).map { |file| File.join(base, file) }
end
::I18n.load_path |= paths
::I18n.available_locales = SUPPORTED
default = ((ENV["VGH_LANG"] || ENV["LANG"] || "en")[0, 2] || "en").to_sym
::I18n.default_locale = SUPPORTED.include?(default) ? default : :en
::I18n.backend.load_translations
@i18n_setup = true
end
|
.setup_locale_from_config!(cfg) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 59
def setup_locale_from_config!(cfg)
lang = (cfg.respond_to?(:locale) ? cfg.locale : nil) || ENV.fetch("VGH_LANG", nil)
return unless lang
set_locale!(lang)
rescue UnsupportedLocaleError
set_locale!("en")
end
|
.t(key, **opts) ⇒ Object
75
76
77
78
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 75
def t(key, **opts)
setup_i18n!
::I18n.t(namespaced(key), **opts)
end
|
.t_hash(key) ⇒ Object
80
81
82
83
84
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 80
def t_hash(key)
setup_i18n!
v = ::I18n.t(namespaced(key), default: {})
v.is_a?(Hash) ? v : {}
end
|
.warn(ui, msg) ⇒ Object
100
101
102
|
# File 'lib/vagrant-git-hooks/helpers.rb', line 100
def warn(ui, msg)
ui&.warn(msg) || puts(msg)
end
|