Module: I18n::Backend::Fallbacks
- Defined in:
- lib/i18n/backend/fallbacks.rb
Instance Method Summary (collapse)
- - (Object) extract_non_symbol_default!(options)
-
- (Object) translate(locale, key, options = {})
Overwrites the Base backend translate method so that it will try each locale given by I18n.fallbacks for the given locale.
Instance Method Details
- (Object) extract_non_symbol_default!(options)
54 55 56 57 58 59 60 61 |
# File 'lib/i18n/backend/fallbacks.rb', line 54 def extract_non_symbol_default!() defaults = [[:default]].flatten first_non_symbol_default = defaults.detect{|default| !default.is_a?(Symbol)} if first_non_symbol_default [:default] = defaults[0, defaults.index(first_non_symbol_default)] end return first_non_symbol_default end |
- (Object) translate(locale, key, options = {})
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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/i18n/backend/fallbacks.rb', line 37 def translate(locale, key, = {}) return super if [:fallback] default = extract_non_symbol_default!() if [:default] [:fallback] = true I18n.fallbacks[locale].each do |fallback| catch(:exception) do result = super(fallback, key, ) return result unless result.nil? end end .delete(:fallback) return super(locale, nil, .merge(:default => default)) if default throw(:exception, I18n::MissingTranslation.new(locale, key, )) end |