Module: I18n::Backend::Fallbacks
- Defined in:
- lib/i18n/backend/fallbacks.rb
Instance Method Summary collapse
- #extract_string_or_lambda_default!(options) ⇒ Object
- #find_first_string_or_lambda_default(defaults) ⇒ Object
-
#translate(locale, key, options = {}) ⇒ Object
Overwrites the Base backend translate method so that it will try each locale given by I18n.fallbacks for the given locale.
Instance Method Details
#extract_string_or_lambda_default!(options) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/i18n/backend/fallbacks.rb', line 56 def extract_string_or_lambda_default!() defaults = [[:default]].flatten if index = find_first_string_or_lambda_default(defaults) [:default] = defaults[0, index] defaults[index] end end |
#find_first_string_or_lambda_default(defaults) ⇒ Object
64 65 66 67 |
# File 'lib/i18n/backend/fallbacks.rb', line 64 def find_first_string_or_lambda_default(defaults) defaults.each_with_index { |default, ix| return ix if String === default || Proc === default } nil end |
#translate(locale, key, options = {}) ⇒ Object
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 raise a MissingTranslationData exception as usual.
The default option takes precedence over fallback locales only when it’s a Symbol. When the default contains a String or a Proc it is evaluated last after all the fallback locales have been tried.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/i18n/backend/fallbacks.rb', line 38 def translate(locale, key, = {}) return super if [:fallback] default = extract_string_or_lambda_default!() if [:default] [:fallback] = true I18n.fallbacks[locale].each do |fallback| begin result = super(fallback, key, ) return result unless result.nil? rescue I18n::MissingTranslationData end end .delete(:fallback) return super(locale, nil, .merge(:default => default)) if default raise(I18n::MissingTranslationData.new(locale, key, )) end |