Skip to content

Commit eae2d1b

Browse files
authored
Merge pull request #537 from stereosupersonic/bugfix-custom-fallback-class
custom fallback class
2 parents 1a4cf20 + 17244f7 commit eae2d1b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/i18n/backend/fallbacks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def fallbacks
2020

2121
# Sets the current fallbacks implementation. Use this to set a different fallbacks implementation.
2222
def fallbacks=(fallbacks)
23-
@@fallbacks = fallbacks.is_a?(I18n::Locale::Fallbacks) ? fallbacks : I18n::Locale::Fallbacks.new(fallbacks)
23+
@@fallbacks = fallbacks.is_a?(Array) ? I18n::Locale::Fallbacks.new(fallbacks) : fallbacks
2424
end
2525
end
2626

test/backend/fallbacks_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,35 @@ def setup
128128
end
129129
end
130130

131+
# See Issue #536
132+
class I18nBackendFallbacksWithCustomClass < I18n::TestCase
133+
class BackendWithFallbacks < I18n::Backend::Simple
134+
include I18n::Backend::Fallbacks
135+
end
136+
137+
# Quacks like a fallback class
138+
class MyDefaultFallback
139+
def [](key)
140+
[:my_language]
141+
end
142+
end
143+
144+
def setup
145+
super
146+
I18n.backend = BackendWithFallbacks.new
147+
I18n.enforce_available_locales = false
148+
I18n.fallbacks = MyDefaultFallback.new
149+
store_translations(:my_language, foo: 'customer foo')
150+
store_translations(:en, foo: 'english foo')
151+
end
152+
153+
test "can use a default fallback object that doesn't inherit from I18n::Locale::Fallbacks" do
154+
assert_equal 'customer foo', I18n.t(:foo, locale: :en)
155+
assert_equal 'customer foo', I18n.t(:foo, locale: :nothing)
156+
end
157+
end
158+
159+
131160
class I18nBackendFallbacksLocalizeTest < I18n::TestCase
132161
class Backend < I18n::Backend::Simple
133162
include I18n::Backend::Fallbacks

0 commit comments

Comments
 (0)