Module: I18n::Backend::Translate

Defined in:
lib/i18n/backend/translate.rb

Overview

It is highly recommended to use Translator wit Fallback plugin

I18n::Backend::Simple.send(:include, I18n::Backend::Translate)
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)

notice that Translator have to be included BEFORE Fallback otherwise the fallback will get Hash (even with empty translation) and won’t work.

Instance Method Summary collapse

Instance Method Details

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

wrapper which can work with both format the simple and the Translator’s

Raises:

  • (InvalidLocale)


20
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
# File 'lib/i18n/backend/translate.rb', line 20

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

  if options.empty?
    entry = resolve(locale, key, entry, options)
  else
    count, default = options.values_at(:count, :default)
    values = options.except(*RESERVED_KEYS)
    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?
  entry = entry.dup if entry.is_a?(String)

  entry = pluralize(locale, entry, count) if count
  entry = translate_to_i18n(entry) # after pluralization there can be enhanced format
  entry = entry.dup if entry.is_a?(String)
  entry = interpolate(locale, entry, values) if values

  #throw(:exception, I18n::MissingTranslation.new(locale, key, options)) if entry.to_s.empty?

  entry
end