Skip to content

Commit

Permalink
Merge pull request #415 from tjoyal/fallbacks-to-exclude-default-locale
Browse files Browse the repository at this point in the history
Fallbacks to exclude default locale
  • Loading branch information
radar authored Apr 18, 2018
2 parents 745fd30 + 8ddfb17 commit 697b42d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
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
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

0 comments on commit 697b42d

Please sign in to comment.