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]
[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
|