Skip to content

Commit

Permalink
Merge pull request #387 from daniel-rikowski/default-nil-fallbacks
Browse files Browse the repository at this point in the history
Fix default: nil with fallbacks
  • Loading branch information
radar committed Oct 10, 2017
2 parents 0c660fc + bc364ae commit 2ae3b70
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/i18n/backend/fallbacks.rb
Expand Up @@ -37,15 +37,16 @@ module Fallbacks
def translate(locale, key, options = {})
return super unless options.fetch(:fallback, true)
return super if options[:fallback_in_progress]
default = extract_non_symbol_default!(options) if options[:default]
original_default = options[:default]
extract_non_symbol_default!(options)

begin
options[:fallback_in_progress] = 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?
return result unless result.nil?
end
rescue I18n::InvalidLocale
# we do nothing when the locale is invalid, as this is a fallback anyways.
Expand All @@ -55,7 +56,7 @@ def translate(locale, key, options = {})
options.delete(:fallback_in_progress)
end

return super(locale, nil, options.merge(:default => default)) if default
return super(locale, nil, options.merge(:default => original_default)) if options.key?(:default)
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
end

Expand Down
5 changes: 5 additions & 0 deletions test/backend/fallbacks_test.rb
Expand Up @@ -59,6 +59,11 @@ def setup
assert_equal({}, I18n.t(:missing_bar, :locale => :'de-DE', :default => {}))
end

test "returns the :de translation for a missing :'de-DE' when :default is nil" do
assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => nil)
assert_nil I18n.t(:missing_bar, :locale => :'de-DE', :default => nil)
end

test "returns the :'de-DE' default :baz translation for a missing :'de-DE' when defaults contains Symbol" do
assert_equal 'Baz in :de-DE', I18n.t(:missing_foo, :locale => :'de-DE', :default => [:baz, "Default Bar"])
end
Expand Down

0 comments on commit 2ae3b70

Please sign in to comment.