Module: I18n

Defined in:
lib/mova-i18n.rb

Class Method Summary collapse

Class Method Details

.exists?(key, locale = config.locale) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/mova-i18n.rb', line 66

def exists?(key, locale = config.locale)
  locale_with_fallbacks = fallbacks[locale]
  result = mova.translator.get([key], locale_with_fallbacks, default: "")
  Mova.presence(result)
end

.fallbacksObject



15
16
17
# File 'lib/mova-i18n.rb', line 15

def self.fallbacks
  @fallbacks ||= Hash.new { |h,k| h[k] = [k] }
end

.movaObject



8
9
10
# File 'lib/mova-i18n.rb', line 8

def self.mova
  Mova::I18nConfig
end

.reload!Object



72
73
74
75
# File 'lib/mova-i18n.rb', line 72

def reload!
  super
  mova.transfer_translations!
end

.translate(key, options = nil) ⇒ Object Also known as: t



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mova-i18n.rb', line 21

def translate(key, options = nil)
  options = options && options.dup || {}

  locale = options[:locale] || config.locale
  locale_with_fallbacks =
    if options[:fallback]
      # suppress locale fallbacks (inverted due to I18n fallbacks implementation)
      [locale]
    else
      fallbacks[locale]
    end

  if (default = options[:default]) && !default.is_a?(Hash)
    defaults = Array(default)
    options[:default] = defaults.last.is_a?(String) ? defaults.pop : nil
    key = Array(key).concat(defaults)
  end

  if (count = options[:count])
    zero_plural_key = :zero if count == 0
    plural_key = mova.pluralizer(locale).call(count)
    key = Array(key).each_with_object([]) do |key, memo|
      memo << Mova::Scope.join(key, zero_plural_key) if zero_plural_key
      memo << Mova::Scope.join(key, plural_key)
      memo << key
    end
  end

  if (scope = options[:scope])
    scope = Array(scope)
    key = Array(key).map do |key|
      Mova::Scope.join(scope + [key])
    end
  end

  result = mova.translator.get(key, locale_with_fallbacks, options)

  if result.is_a?(String) && !(interpolation_keys = options.keys - RESERVED_KEYS).empty?
    mova.interpolator.call(result, options)
  else
    result
  end
end