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
EMOJI =
{
  success:  "",
  info:     "🔍",
  ongoing:  "🔁",
  warning:  "⚠️",
  error:    "",
  version:  "💾",
  broom:    "🧹",
  question: ""
}.freeze

Class Method Summary collapse

Class Method Details

.e(key, no_emoji: false) ⇒ Object



55
56
57
58
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 55

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

.our_key?(k) ⇒ Boolean



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

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


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

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


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

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

.set_locale!(lang) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/vagrant-docker-networks-manager/helpers.rb', line 44

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



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

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")]
  ::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

.t(key, **opts) ⇒ Object



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

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

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



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

def t!(key, **opts)
  setup_i18n!
  k = key.to_s
  if our_key?(k) && !::I18n.exists?(k, ::I18n.locale)
    raise MissingTranslationError, "#{EMOJI[:error]} [#{::I18n.locale}] Missing translation for key: #{k}"
  end
  ::I18n.t(k, **opts)
end

.t_hash(key) ⇒ Object



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

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