Module: VagrantDockerNetworksManager::UiHelpers

Defined in:
lib/vagrant-docker-networks-manager/helpers.rb

Defined Under Namespace

Classes: MissingTranslationError, UnsupportedLocaleError

Constant Summary collapse

SUPPORTED =
[:en, :fr].freeze
OUR_NAMESPACES =
%w[messages. errors. usage. help. prompts. log. emoji.].freeze
NS =
"vdnm"
EMOJI =
{
  success:  "โœ…",
  info:     "๐Ÿ”",
  ongoing:  "๐Ÿ”",
  warning:  "โš ๏ธ",
  error:    "โŒ",
  version:  "๐Ÿ’พ",
  broom:    "๐Ÿงน",
  question: "โ“",
  bug:      "๐Ÿ›"
}.freeze

Class Method Summary collapse

Class Method Details

.debug(ui, msg) ⇒ Object



122
123
124
125
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 122

def debug(ui, msg)
  return unless debug_enabled?
  say(ui, "#{e(:bug)} #{msg}")
end

.debug_enabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 118

def debug_enabled?
  ENV["VDNM_DEBUG"].to_s == "1"
end

.e(key, no_emoji: false) ⇒ Object



101
102
103
104
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 101

def e(key, no_emoji: false)
  return "" if no_emoji || ENV["VDNM_NO_EMOJI"] == "1"
  EMOJI[key] || ""
end

.error(ui, msg) ⇒ Object



114
115
116
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 114

def error(ui, msg)
  ui&.error(msg) || warn(ui, msg)
end

.exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 93

def exists?(key)
  ::I18n.exists?(namespaced(key), ::I18n.locale)
end

.namespaced(key) ⇒ Object



68
69
70
71
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 68

def namespaced(key)
  k = key.to_s
  k.start_with?("#{NS}.") ? k : "#{NS}.#{k}"
end

.our_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 97

def our_key?(k)
  OUR_NAMESPACES.any? { |ns| k.start_with?(ns) }
end


127
128
129
130
131
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 127

def print_general_help
  setup_i18n!
  puts t("help.general_title")
  t_hash("help.commands").each_value { |line| puts "  #{line}" }
end


133
134
135
136
137
138
139
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 133

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



106
107
108
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 106

def say(ui, msg)
  ui&.info(msg) || puts(msg)
end

.set_locale!(lang) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 49

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 29

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["VDNM_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



60
61
62
63
64
65
66
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 60

def setup_locale_from_config!(cfg)
  lang = cfg.locale || ENV["VDNM_LANG"]
  return unless lang
  set_locale!(lang)
rescue UnsupportedLocaleError
  set_locale!("en")
end

.t(key, **opts) ⇒ Object



73
74
75
76
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 73

def t(key, **opts)
  setup_i18n!
  ::I18n.t(namespaced(key), **opts)
end

.t!(key, **opts) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 78

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



87
88
89
90
91
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 87

def t_hash(key)
  setup_i18n!
  v = ::I18n.t(namespaced(key), default: {})
  v.is_a?(Hash) ? v : {}
end

.warn(ui, msg) ⇒ Object



110
111
112
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 110

def warn(ui, msg)
  ui&.warn(msg) || puts(msg)
end