Module: I18nema::CoreMethods

Included in:
Backend
Defined in:
lib/i18nema.rb

Constant Summary collapse

RESERVED_KEY_MAP =
Hash[I18n::RESERVED_KEYS.map{|k|[k,true]

Instance Method Summary collapse

Instance Method Details

#translate(locale, key, options = {}) ⇒ Object

Raises:

  • (I18n::InvalidLocale)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/i18nema.rb', line 11

def translate(locale, key, options = {})
  raise I18n::InvalidLocale.new(locale) unless locale
  entry = key && lookup(locale, key, options[:scope], options)

  if options.empty?
    entry = resolve(locale, key, entry, options)
  else
    count, default = options.values_at(:count, :default)
    # significant speedup over Hash#except
    values = options.reject { |key, value| RESERVED_KEY_MAP.key?(key) }
    entry = entry.nil? && default ?
      default(locale, key, default, options) : resolve(locale, key, entry, options)
  end

  throw(:exception, I18n::MissingTranslation.new(locale, key, options)) if entry.nil?
  # no need to dup, since I18nema gives us a new string

  entry = pluralize(locale, entry, count) if count
  entry = interpolate(locale, entry, values) if values
  entry
end