Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when including Fallbacks on non-Simple backend #260

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we having two fallbacks being triggered now? One being included in Chain, other in the Backend class being used here. Does this affect anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, it would call fallback 2x. But I am sure this is not an issue. I wanted to add the send(:include, I18n::Backend::Fallbacks) to the root backend because thats what rails does.
I probably could refactor those tests, to simulate situations similar to the ones that happen on rails code.

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