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

custom fallback class #537

Merged
merged 5 commits into from Jul 23, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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