Module: I18n::Backend::Fallbacks
- Defined in:
- lib/i18n/backend/fallbacks.rb
Instance Method Summary collapse
- #exists?(locale, key, options = EMPTY_HASH) ⇒ Boolean
- #extract_non_symbol_default!(options) ⇒ Object
- #resolve_entry(locale, object, subject, options = EMPTY_HASH) ⇒ Object
-
#translate(locale, key, options = EMPTY_HASH) ⇒ 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
#exists?(locale, key, options = EMPTY_HASH) ⇒ Boolean
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/i18n/backend/fallbacks.rb', line 98 def exists?(locale, key, = EMPTY_HASH) return super unless .fetch(:fallback, true) I18n.fallbacks[locale].each do |fallback| begin return true if super(fallback, key, ) rescue I18n::InvalidLocale # we do nothing when the locale is invalid, as this is a fallback anyways. end end false end |
#extract_non_symbol_default!(options) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/i18n/backend/fallbacks.rb', line 89 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 |
#resolve_entry(locale, object, subject, options = EMPTY_HASH) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/i18n/backend/fallbacks.rb', line 67 def resolve_entry(locale, object, subject, = EMPTY_HASH) return subject if [:resolve] == false result = catch(:exception) do .delete(:fallback_in_progress) if .key?(:fallback_in_progress) case subject when Symbol I18n.translate(subject, **.merge( :locale => [:fallback_original_locale], :throw => true, :skip_interpolation => true )) when Proc date_or_time = .delete(:object) || object resolve_entry([:fallback_original_locale], object, subject.call(date_or_time, **)) else subject end end result unless result.is_a?(MissingTranslation) end |
#translate(locale, key, options = EMPTY_HASH) ⇒ 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 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/i18n/backend/fallbacks.rb', line 41 def translate(locale, key, = EMPTY_HASH) return super unless .fetch(:fallback, true) return super if [:fallback_in_progress] default = extract_non_symbol_default!() if [:default] = .merge(:fallback_in_progress => true, fallback_original_locale: locale) I18n.fallbacks[locale].each do |fallback| begin catch(:exception) do result = super(fallback, key, ) unless result.nil? on_fallback(locale, fallback, key, ) if locale.to_s != fallback.to_s return result end end rescue I18n::InvalidLocale # we do nothing when the locale is invalid, as this is a fallback anyways. end end return if .key?(:default) && [:default].nil? return super(locale, nil, .merge(:default => default)) if default throw(:exception, I18n::MissingTranslation.new(locale, key, )) end |