Skip to content

Commit

Permalink
Merge pull request #260 from arthurnn/fix_fallback_ensure_locale
Browse files Browse the repository at this point in the history
Fix error when including Fallbacks on non-Simple backend
  • Loading branch information
carlosantoniodasilva committed May 31, 2014
2 parents 667bcfe + f4d1d8f commit 1e983c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/i18n/backend/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1e983c0

Please sign in to comment.