Skip to content

Commit

Permalink
Merge pull request #537 from stereosupersonic/bugfix-custom-fallback-…
Browse files Browse the repository at this point in the history
…class

custom fallback class
  • Loading branch information
radar committed Jul 23, 2020
2 parents 1a4cf20 + 17244f7 commit eae2d1b
Show file tree
Hide file tree
Showing 2 changed files with 30 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.is_a?(I18n::Locale::Fallbacks) ? fallbacks : I18n::Locale::Fallbacks.new(fallbacks)
@@fallbacks = fallbacks.is_a?(Array) ? I18n::Locale::Fallbacks.new(fallbacks) : fallbacks
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/backend/fallbacks_test.rb
Expand Up @@ -128,6 +128,35 @@ def setup
end
end

# See Issue #536
class I18nBackendFallbacksWithCustomClass < I18n::TestCase
class BackendWithFallbacks < I18n::Backend::Simple
include I18n::Backend::Fallbacks
end

# Quacks like a fallback class
class MyDefaultFallback
def [](key)
[:my_language]
end
end

def setup
super
I18n.backend = BackendWithFallbacks.new
I18n.enforce_available_locales = false
I18n.fallbacks = MyDefaultFallback.new
store_translations(:my_language, foo: 'customer foo')
store_translations(:en, foo: 'english foo')
end

test "can use a default fallback object that doesn't inherit from I18n::Locale::Fallbacks" do
assert_equal 'customer foo', I18n.t(:foo, locale: :en)
assert_equal 'customer foo', I18n.t(:foo, locale: :nothing)
end
end


class I18nBackendFallbacksLocalizeTest < I18n::TestCase
class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
Expand Down

0 comments on commit eae2d1b

Please sign in to comment.