Skip to content

Commit

Permalink
Merge branch 'pr-304'
Browse files Browse the repository at this point in the history
* pr-304:
  Fix fallbacks when translating key arrays

Fixes #304
Fixes #104
  • Loading branch information
radar committed Nov 15, 2016
2 parents c54be4e + 506e2a5 commit 1e47f92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/i18n/backend/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ def translate(locale, key, options = {})
return super if options[:fallback]
default = extract_non_symbol_default!(options) if options[:default]

options[:fallback] = true
I18n.fallbacks[locale].each do |fallback|
begin
catch(:exception) do
result = super(fallback, key, options)
return result if (result.nil? && options.key?(:default) && options[:default].nil?) || !result.nil?
begin
options[:fallback] = true
I18n.fallbacks[locale].each do |fallback|
begin
catch(:exception) do
result = super(fallback, key, options)
return result if (result.nil? && options.key?(:default) && options[:default].nil?) || !result.nil?
end
rescue I18n::InvalidLocale
# we do nothing when the locale is invalid, as this is a fallback anyways.
end
rescue I18n::InvalidLocale
# we do nothing when the locale is invalid, as this is a fallback anyways.
end
ensure
options.delete(:fallback)
end
options.delete(:fallback)

return super(locale, nil, options.merge(:default => default)) if default
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
Expand Down
4 changes: 4 additions & 0 deletions test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def setup
assert_equal 'FOO', I18n.t(:foo, :locale => :'de-DE')
end

test "falls back from de-DE to de when there is no translation for de-DE available when using arrays, too" do
assert_equal ['FOO', 'FOO'], I18n.t([:foo, :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
Expand Down

0 comments on commit 1e47f92

Please sign in to comment.