# File lib/i18n/backend/fallbacks.rb, line 54 54: def extract_string_or_lambda_default!(options) 55: defaults = [options[:default]].flatten 56: if index = find_first_string_or_lambda_default(defaults) 57: options[:default] = defaults[0, index] 58: defaults[index] 59: end 60: end
# File lib/i18n/backend/fallbacks.rb, line 62 62: def find_first_string_or_lambda_default(defaults) 63: defaults.each_with_index { |default, ix| return ix if String === default || Proc === default } 64: nil 65: end
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 or a Proc it is evaluated last after all the fallback locales have been tried.
# File lib/i18n/backend/fallbacks.rb, line 37 37: def translate(locale, key, options = {}) 38: return super if options[:fallback] 39: default = extract_string_or_lambda_default!(options) if options[:default] 40: 41: options[:fallback] = true 42: I18n.fallbacks[locale].each do |fallback| 43: catch(:exception) do 44: result = super(fallback, key, options) 45: return result unless result.nil? 46: end 47: end 48: options.delete(:fallback) 49: 50: return super(locale, nil, options.merge(:default => default)) if default 51: throw(:exception, I18n::MissingTranslation.new(locale, key, options)) 52: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.