Module: VagrantDockerHostsManager::UiHelpers
- Defined in:
- lib/vagrant-docker-hosts-manager/helpers.rb
Defined Under Namespace
Classes: MissingTranslationError, UnsupportedLocaleError
Constant Summary
collapse
- SUPPORTED =
[:en, :fr].freeze
- OUR_NAMESPACES =
%w[messages. errors. log. help.].freeze
- NS =
"vdhm"
- EMOJI =
{
success: "โ
",
info: "๐",
ongoing: "๐",
warning: "โ ๏ธ",
error: "โ",
version: "๐พ",
broom: "๐งน",
question: "โ",
bug: "๐"
}.freeze
Class Method Summary
collapse
Class Method Details
.debug(ui, msg) ⇒ Object
121
122
123
124
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 121
def debug(ui, msg)
return unless debug_enabled?
say(ui, "#{e(:bug)} #{msg}")
end
|
.debug_enabled? ⇒ Boolean
117
118
119
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 117
def debug_enabled?
ENV["VDHM_DEBUG"].to_s == "1"
end
|
.e(key, no_emoji: false) ⇒ Object
100
101
102
103
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 100
def e(key, no_emoji: false)
return "" if no_emoji || ENV["VDHM_NO_EMOJI"] == "1"
EMOJI[key] || ""
end
|
.error(ui, msg) ⇒ Object
113
114
115
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 113
def error(ui, msg)
ui&.error(msg) || warn(ui, msg)
end
|
.exists?(key) ⇒ Boolean
92
93
94
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 92
def exists?(key)
::I18n.exists?(key, ::I18n.locale)
end
|
.namespaced(key) ⇒ Object
67
68
69
70
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 67
def namespaced(key)
k = key.to_s
k.start_with?("#{NS}.") ? k : "#{NS}.#{k}"
end
|
.our_key?(k) ⇒ Boolean
96
97
98
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 96
def our_key?(k)
OUR_NAMESPACES.any? { |ns| k.start_with?(ns) }
end
|
.say(ui, msg) ⇒ Object
105
106
107
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 105
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-docker-hosts-manager/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-docker-hosts-manager/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["VDHM_LANG"] || ENV["LANG"] || "en")[0, 2] rescue "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
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 59
def setup_locale_from_config!(cfg)
lang = (cfg.respond_to?(:locale) ? cfg.locale : nil) || ENV["VDHM_LANG"]
return unless lang
set_locale!(lang)
rescue UnsupportedLocaleError
set_locale!("en")
end
|
.t(key, **opts) ⇒ Object
72
73
74
75
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 72
def t(key, **opts)
setup_i18n!
::I18n.t(namespaced(key), **opts)
end
|
.t!(key, **opts) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 77
def t!(key, **opts)
setup_i18n!
nk = namespaced(key)
if our_key?(key.to_s) && !::I18n.exists?(nk, ::I18n.locale)
raise MissingTranslationError, "#{EMOJI[:error]} [#{::I18n.locale}] Missing translation for key: #{nk}"
end
::I18n.t(nk, **opts)
end
|
.t_hash(key) ⇒ Object
86
87
88
89
90
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 86
def t_hash(key)
setup_i18n!
v = ::I18n.t(namespaced(key), default: {})
v.is_a?(Hash) ? v : {}
end
|
.warn(ui, msg) ⇒ Object
109
110
111
|
# File 'lib/vagrant-docker-hosts-manager/helpers.rb', line 109
def warn(ui, msg)
ui&.warn(msg) || puts(msg)
end
|