module I18n::Backend::Fallbacks - i18n-0.6.11 Documentation

# module I18n::Backend::Fallbacks

### Public Instance Methods

extract_non_symbol_default!(options) click to toggle source

    # File lib/i18n/backend/fallbacks.rb, line 58
    def extract_non_symbol_default!(options)
      defaults = [options[:default]].flatten
      first_non_symbol_default = defaults.detect{|default| !default.is_a?(Symbol)}
      if first_non_symbol_default
        options[:default] = defaults[0, defaults.index(first_non_symbol_default)]
      end
      return first_non_symbol_default
    end

translate(locale, key, options = {}) click to toggle source

Overwrites the Base backend translate method so that it will try each locale given by I18n#fallbacks for the given locale. E.g. for the locale :“de-DE” it might try the locales :“de-DE”, :de and :en (depends on the fallbacks implementation) until it finds a result with the given options. If it does not find any result for any of the locales it will then throw MissingTranslation as usual.

The default option takes precedence over fallback locales only when it's a Symbol. When the default contains a String, Proc or Hash it is evaluated last after all the fallback locales have been tried.

Calls superclass method

    # File lib/i18n/backend/fallbacks.rb, line 37
    def translate(locale, key, options = {})
      return super if options[:fallback]
      default = extract_non_symbol_default!(options) if options[:default]

      options[:fallback] = true
      I18n.fallbacks[locale].each do |fallback|
        begin
          catch(:exception) do
            result = super(fallback, key, options)
            return result unless result.nil?
          end
        rescue I18n::InvalidLocale
          # we do nothing when the locale is invalid, as this is a fallback anyways.
        end
      end
      options.delete(:fallback)

      return super(locale, nil, options.merge(:default => default)) if default
      throw(:exception, I18n::MissingTranslation.new(locale, key, options))
    end