Module: Mova::I18nBridge::Translator

Included in:
Mova::I18nTranslator
Defined in:
lib/mova-i18n/bridge.rb

Instance Method Summary collapse

Instance Method Details

#default(locales, keys, options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mova-i18n/bridge.rb', line 6

def default(locales, keys, options)
  origin_locale = locales.first
  origin_key = keys.first

  if options[:raise]
    raise I18n::MissingTranslationData.new(origin_locale, origin_key, options)
  end

  if options[:throw]
    throw :exception, I18n::MissingTranslation.new(origin_locale, origin_key, options)
  end

  key_with_locale = Mova::Scope.join(origin_locale, origin_key)
  "missing translation: #{key_with_locale}"
end

#put(translations) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/mova-i18n/bridge.rb', line 22

def put(translations)
  super
  put_exact_translation(translations, "i18n.transliterate.rule")
  put_exact_translation(translations, "number.format")
  put_exact_translation(translations, "number.currency.format")
  put_exact_translation(translations, "number.human.format")
  put_exact_translation(translations, "number.human.decimal_units.units")
  put_exact_translation(translations, "number.percentage.format")
  put_exact_translation(translations, "number.precision.format")
end

#put_exact_translation(translations, key) ⇒ Object

Stores exact value because Translator#put flattens hashes before writing to the storage, while ‘I18n.transliterate` and Rails localization helpers rely on ability of storing hashes as is.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mova-i18n/bridge.rb', line 36

def put_exact_translation(translations, key)
  scope_path = Mova::Scope.split(key).map &:to_sym
  locales = translations.keys

  locales.each do |locale|
    full_path = [locale] + scope_path

    translation = full_path.inject(translations) do |memo, scope|
      memo[scope] || {}
    end

    unless translation == {}
      key_with_locale = Mova::Scope.join(full_path)
      storage.write(key_with_locale, translation)
    end
  end
end