diff --git a/lib/i18n/backend/base.rb b/lib/i18n/backend/base.rb index 77e832c0..4cbcc3c0 100644 --- a/lib/i18n/backend/base.rb +++ b/lib/i18n/backend/base.rb @@ -35,7 +35,7 @@ def translate(locale, key, options = EMPTY_HASH) if entry.nil? && options.key?(:default) entry = default(locale, key, options[:default], options) else - entry = resolve(locale, key, entry, options) + entry = resolve_entry(locale, key, entry, options) end count = options[:count] @@ -154,6 +154,7 @@ def resolve(locale, object, subject, options = EMPTY_HASH) end result unless result.is_a?(MissingTranslation) end + alias_method :resolve_entry, :resolve # Picks a translation from a pluralized mnemonic subkey according to English # pluralization rules : diff --git a/lib/i18n/backend/fallbacks.rb b/lib/i18n/backend/fallbacks.rb index acab6dfe..7afbfe3a 100644 --- a/lib/i18n/backend/fallbacks.rb +++ b/lib/i18n/backend/fallbacks.rb @@ -64,13 +64,20 @@ def translate(locale, key, options = EMPTY_HASH) throw(:exception, I18n::MissingTranslation.new(locale, key, options)) end - def resolve(locale, object, subject, options = EMPTY_HASH) + def resolve_entry(locale, object, subject, options = EMPTY_HASH) return subject if options[:resolve] == false - return super unless subject.is_a?(Symbol) - result = catch(:exception) do - options.delete(:fallback_in_progress) - I18n.translate(subject, **options.merge(locale: options[:fallback_original_locale], throw: true)) + options.delete(:fallback_in_progress) if options.key?(:fallback_in_progress) + + case subject + when Symbol + I18n.translate(subject, **options.merge(:locale => options[:fallback_original_locale], :throw => true)) + when Proc + date_or_time = options.delete(:object) || object + resolve_entry(options[:fallback_original_locale], object, subject.call(date_or_time, **options)) + else + subject + end end result unless result.is_a?(MissingTranslation) end diff --git a/lib/i18n/backend/simple.rb b/lib/i18n/backend/simple.rb index 687c1154..0c49de8f 100644 --- a/lib/i18n/backend/simple.rb +++ b/lib/i18n/backend/simple.rb @@ -94,7 +94,7 @@ def lookup(locale, key, scope = [], options = EMPTY_HASH) return nil unless result.has_key?(_key) end result = result[_key] - result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol) + result = resolve_entry(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol) result end end diff --git a/test/backend/fallbacks_test.rb b/test/backend/fallbacks_test.rb index 04f3497a..a215f73a 100644 --- a/test/backend/fallbacks_test.rb +++ b/test/backend/fallbacks_test.rb @@ -239,6 +239,28 @@ def setup end end +# See Issue #617 +class RegressionTestFor617 < I18n::TestCase + class Backend < I18n::Backend::Simple + include I18n::Backend::Fallbacks + end + + def setup + super + I18n.backend = Backend.new + I18n.enforce_available_locales = false + I18n.fallbacks = {:en=>[:en], :"en-US"=>[:"en-US", :en]} + I18n.locale = :'en-US' + store_translations(:"en-US", {}) + store_translations(:en, :activerecord=>{:models=>{:product=>{:one=>"Product", :other=>"Products"}, :"product/ticket"=>{:one=>"Ticket", :other=>"Tickets"}}}) + end + + test 'model scope resolution' do + defaults = [:product, "Ticket"] + options = {:scope=>[:activerecord, :models], :count=>1, :default=> defaults} + assert_equal("Ticket", I18n.t(:"product/ticket", **options)) + end +end class I18nBackendFallbacksLocalizeTest < I18n::TestCase class Backend < I18n::Backend::Simple