Skip to content

Commit

Permalink
Merge pull request #535 from ruby-i18n/fallbacks
Browse files Browse the repository at this point in the history
Always instantiate I18n::Locale::Fallbacks objects when using I18n.fallbacks=
  • Loading branch information
radar committed Jul 20, 2020
2 parents c2d85c0 + 1cb0987 commit 360b8b7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/i18n/backend/fallbacks.rb
Expand Up @@ -20,7 +20,7 @@ def fallbacks

# Sets the current fallbacks implementation. Use this to set a different fallbacks implementation.
def fallbacks=(fallbacks)
@@fallbacks = fallbacks
@@fallbacks = fallbacks.is_a?(I18n::Locale::Fallbacks) ? fallbacks : I18n::Locale::Fallbacks.new(fallbacks)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/locale/fallbacks.rb
Expand Up @@ -26,7 +26,7 @@
#
# I18n.default_locale = :"en-US"
# I18n.fallbacks = I18n::Locale::Fallbacks.new(:"de-AT" => :"de-DE")
# I18n.fallbacks[:"de-AT"] # => [:"de-AT", :"de-DE", :de, :"en-US", :en]
# I18n.fallbacks[:"de-AT"] # => [:"de-AT", :de, :"de-DE"]
#
# # using a custom locale as default fallback locale
#
Expand Down
19 changes: 19 additions & 0 deletions test/backend/fallbacks_test.rb
Expand Up @@ -109,6 +109,25 @@ def setup
end
end

# See Issue #534
class I18nBackendFallbacksLocalizeTestWithDefaultLocale < I18n::TestCase
class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
end

def setup
super
I18n.backend = Backend.new
I18n.enforce_available_locales = false
I18n.fallbacks = [I18n.default_locale]
store_translations(:en, time: { formats: { fallback: 'en fallback' } })
end

test "falls back to default locale - Issue #534" do
assert_equal 'en fallback', I18n.l(Time.now, format: :fallback, locale: "un-supported")
end
end

class I18nBackendFallbacksLocalizeTest < I18n::TestCase
class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
Expand Down
20 changes: 20 additions & 0 deletions test/locale/fallbacks_test.rb
Expand Up @@ -9,6 +9,26 @@ class I18nFallbacksDefaultsTest < I18n::TestCase
assert_equal [], fallbacks.defaults
end

test "documentation example #1 - does not use default locale in fallbacks - See Issues #413 & #415" do
I18n.default_locale = :"en-US"
fallbacks = Fallbacks.new(:"de-AT" => :"de-DE")
assert_equal [:"de-AT", :de, :"de-DE"], fallbacks[:"de-AT"]
end

test "documentation example #2 - does not use default locale in fallbacks - Uses custom locale - See Issues #413 & #415" do
I18n.default_locale = :"en-US"
fallbacks = Fallbacks.new(:"en-GB", :"de-AT" => :de, :"de-CH" => :de)
assert_equal [:"de-AT", :de, :"en-GB", :en], fallbacks[:"de-AT"]
assert_equal [:"de-CH", :de, :"en-GB", :en], fallbacks[:"de-CH"]
end

test "explicit fallback to default locale" do
I18n.default_locale = :"en-US"
fallbacks = Fallbacks.new([:"en-US"])
assert_equal [:"de-AT", :de, :"en-US", :en], fallbacks[:"de-AT"]
assert_equal [:"de-CH", :de, :"en-US", :en], fallbacks[:"de-CH"]
end

test "defaults reflect a manually passed default locale if any" do
fallbacks = Fallbacks.new(:'fi-FI')
assert_equal [:'fi-FI', :fi], fallbacks.defaults
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Expand Up @@ -34,6 +34,7 @@ def teardown
I18n.backend = nil
I18n.default_separator = nil
I18n.enforce_available_locales = true
I18n.fallbacks = nil
super
end

Expand Down

0 comments on commit 360b8b7

Please sign in to comment.