Skip to content

Commit

Permalink
Always instantiate I18n::Locale::Fallbacks objects when using I18n.fa…
Browse files Browse the repository at this point in the history
…llbacks=
  • Loading branch information
radar committed Jul 15, 2020
1 parent dfd1e29 commit 741845c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
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
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: { long: 'en fallback' } })
end

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

class I18nBackendFallbacksLocalizeTest < I18n::TestCase
class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
Expand Down
7 changes: 7 additions & 0 deletions test/locale/fallbacks_test.rb
Expand Up @@ -22,6 +22,13 @@ class I18nFallbacksDefaultsTest < I18n::TestCase
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 741845c

Please sign in to comment.