diff --git a/lib/i18n/backend/fallbacks.rb b/lib/i18n/backend/fallbacks.rb index e55e0226..d74b800c 100644 --- a/lib/i18n/backend/fallbacks.rb +++ b/lib/i18n/backend/fallbacks.rb @@ -40,10 +40,13 @@ def translate(locale, key, options = {}) options[:fallback] = true I18n.fallbacks[locale].each do |fallback| - next unless translations.keys.include?(fallback) - catch(:exception) do - result = super(fallback, key, options) - return result unless result.nil? + begin + catch(:exception) do + result = super(fallback, key, options) + return result unless result.nil? + end + rescue I18n::InvalidLocale + # we do nothing when the locale is invalid, as this is a fallback anyways. end end options.delete(:fallback) diff --git a/test/backend/fallbacks_test.rb b/test/backend/fallbacks_test.rb index 82f45af9..8baeed6c 100644 --- a/test/backend/fallbacks_test.rb +++ b/test/backend/fallbacks_test.rb @@ -126,10 +126,21 @@ class Backend < I18n::Backend::Simple def setup backend = Backend.new backend.store_translations(:de, :foo => 'FOO') + backend.store_translations(:'pt-BR', :foo => 'Baz in :pt-BR') I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, backend) + I18n.backend.class.send(:include, I18n::Backend::Fallbacks) end test "falls back from de-DE to de when there is no translation for de-DE available" do assert_equal 'FOO', I18n.t(:foo, :locale => :'de-DE') end + + test "should not raise error when enforce_available_locales is true, :'pt' is missing and default is a Symbol" do + I18n.enforce_available_locales = true + begin + assert_equal 'Foo', I18n.t(:'model.attrs.foo', :locale => :'pt-BR', :default => [:'attrs.foo', "Foo"]) + ensure + I18n.enforce_available_locales = false + end + end end