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

Fallbacks to exclude default locale #415

Merged
merged 1 commit into from
Apr 18, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/i18n/locale/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Fallbacks < Hash
def initialize(*mappings)
@map = {}
map(mappings.pop) if mappings.last.is_a?(Hash)
self.defaults = mappings.empty? ? [I18n.default_locale.to_sym] : mappings
self.defaults = mappings.empty? ? [] : mappings
Copy link
Contributor

Choose a reason for hiding this comment

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

@tjoyal This PR did not update the documentation comment above, which still references default_locale.

Took me far too long to realize that default_locale was no longer getting used. 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

I've opened a PR to update the comment: #576

end

def defaults=(defaults)
Expand Down
23 changes: 5 additions & 18 deletions test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,10 @@ def setup
assert_equal 'Interpolate %{value}', I18n.t(:interpolate)
end

test "returns the :en translation for a missing :de translation" do
assert_equal 'Foo in :en', I18n.t(:foo, :locale => :de)
end

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

test "returns the :en translation for translation missing in both :de and :'de-De'" do
assert_equal 'Buz in :en', I18n.t(:buz, :locale => :'de-DE')
end

test "returns the :de translation for a missing :'de-DE' when :default is a String" do
assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => "Default Bar")
assert_equal "Default Bar", I18n.t(:missing_bar, :locale => :'de-DE', :default => "Default Bar")
Expand Down Expand Up @@ -117,18 +109,13 @@ def setup
super
I18n.backend = Backend.new
store_translations(:en, :date => { :formats => { :en => 'en' }, :day_names => %w(Sunday) })
store_translations(:de, :date => { :formats => { :de => 'de' } })
store_translations(:de, :date => { :formats => { :de => 'de' }, :day_names => %w(Sunday) })
end

test "still uses an existing format as usual" do
assert_equal 'en', I18n.l(Date.today, :format => :en, :locale => :en)
end

test "looks up and uses a fallback locale's format for a key missing in the given locale (1)" do
assert_equal 'en', I18n.l(Date.today, :format => :en, :locale => :de)
end

test "looks up and uses a fallback locale's format for a key missing in the given locale (2)" do
test "looks up and uses a fallback locale's format for a key missing in the given locale" do
assert_equal 'de', I18n.l(Date.today, :format => :de, :locale => :'de-DE')
end

Expand All @@ -137,7 +124,7 @@ def setup
end

test "uses a fallback locale's translation for a key missing in the given locale" do
assert_equal 'Sunday', I18n.l(Date.new(2010, 1, 3), :format => '%A', :locale => :de)
assert_equal 'Sunday', I18n.l(Date.new(2010, 1, 3), :format => '%A', :locale => :'de-DE')
end
end

Expand Down Expand Up @@ -208,8 +195,8 @@ def setup
end

test "exists? should return true given a key which is missing from the given locale and exists in a fallback locale" do
assert_equal true, I18n.exists?(:foo, :de)
assert_equal true, I18n.exists?(:foo, :'de-DE')
assert_equal true, I18n.exists?(:bar, :de)
assert_equal true, I18n.exists?(:bar, :'de-DE')
end

test "exists? should return false given a key which is missing from the given locale and all its fallback locales" do
Expand Down
4 changes: 2 additions & 2 deletions test/locale/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
include I18n::Locale

class I18nFallbacksDefaultsTest < I18n::TestCase
test "defaults reflect the I18n.default_locale if no default has been set manually" do
test "defaults to an empty array if no default has been set manually" do
I18n.default_locale = :'en-US'
fallbacks = Fallbacks.new
assert_equal [:'en-US', :en], fallbacks.defaults
assert_equal [], fallbacks.defaults
end

test "defaults reflect a manually passed default locale if any" do
Expand Down