Skip to content

Commit 77c26aa

Browse files
committed
Fix Chained backend with KeyValue
1 parent 7c6ccf4 commit 77c26aa

File tree

4 files changed

+80
-11
lines changed

4 files changed

+80
-11
lines changed

lib/i18n/backend/base.rb

+19-9
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,21 @@ def translate(locale, key, options = {})
3434
entry = resolve(locale, key, entry, options)
3535
end
3636

37-
entry = entry.dup if entry.is_a?(String)
38-
3937
count = options[:count]
40-
entry = pluralize(locale, entry, count) if count
4138

42-
if entry.nil?
39+
if entry.nil? && (subtrees? || !count)
4340
if (options.key?(:default) && !options[:default].nil?) || !options.key?(:default)
4441
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
4542
end
4643
end
4744

45+
entry = entry.dup if entry.is_a?(String)
46+
entry = pluralize(locale, entry, count) if count
47+
48+
if entry.nil? && !subtrees?
49+
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
50+
end
51+
4852
deep_interpolation = options[:deep_interpolation]
4953
values = options.except(*RESERVED_KEYS)
5054
if values
@@ -97,6 +101,10 @@ def lookup(locale, key, scope = [], options = {})
97101
raise NotImplementedError
98102
end
99103

104+
def subtrees?
105+
true
106+
end
107+
100108
# Evaluates defaults.
101109
# If given subject is an Array, it walks the array and returns the
102110
# first translation that can be resolved. Otherwise it tries to resolve
@@ -145,8 +153,7 @@ def resolve(locale, object, subject, options = {})
145153
def pluralize(locale, entry, count)
146154
return entry unless entry.is_a?(Hash) && count
147155

148-
key = :zero if count == 0 && entry.has_key?(:zero)
149-
key ||= count == 1 ? :one : :other
156+
key = pluralization_key(entry, count)
150157
raise InvalidPluralizationData.new(entry, count, key) unless entry.has_key?(key)
151158
entry[key]
152159
end
@@ -161,9 +168,7 @@ def pluralize(locale, entry, count)
161168
# each element of the array is recursively interpolated (until it finds a string)
162169
# method interpolates ["yes, %{user}", ["maybe no, %{user}, "no, %{user}"]], :user => "bartuz"
163170
# # => "["yes, bartuz",["maybe no, bartuz", "no, bartuz"]]"
164-
165-
166-
def interpolate(locale, subject, values = {})
171+
def interpolate(locale, subject, values = {})
167172
return subject if values.empty?
168173

169174
case subject
@@ -240,6 +245,11 @@ def translate_localization_format(locale, object, format, options)
240245
end
241246
end
242247
end
248+
249+
def pluralization_key(entry, count)
250+
key = :zero if count == 0 && entry.has_key?(:zero)
251+
key ||= count == 1 ? :one : :other
252+
end
243253
end
244254
end
245255
end

lib/i18n/backend/key_value.rb

+17-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def available_locales
103103

104104
protected
105105

106+
def subtrees?
107+
@subtrees
108+
end
109+
106110
def lookup(locale, key, scope = [], options = {})
107111
key = normalize_flat_keys(locale, key, scope, options[:separator])
108112
value = @store["#{locale}.#{key}"]
@@ -116,6 +120,15 @@ def lookup(locale, key, scope = [], options = {})
116120
SubtreeProxy.new("#{locale}.#{key}", @store)
117121
end
118122
end
123+
124+
def pluralize(locale, entry, count)
125+
if subtrees?
126+
super
127+
else
128+
key = pluralization_key(entry, count)
129+
entry[key]
130+
end
131+
end
119132
end
120133

121134
class SubtreeProxy
@@ -132,7 +145,10 @@ def has_key?(key)
132145
def [](key)
133146
unless @subtree && value = @subtree[key]
134147
value = @store["#{@master_key}.#{key}"]
135-
(@subtree ||= {})[key] = JSON.decode(value) if value
148+
if value
149+
value = JSON.decode(value)
150+
(@subtree ||= {})[key] = value
151+
end
136152
end
137153
value
138154
end

test/backend/chain_test.rb

+32-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def setup
1313
})
1414
@second = backend(:en => {
1515
:bar => 'Bar', :formats => {
16-
:long => 'long',
16+
:long => 'long',
1717
:subformats => {:long => 'long'},
1818
},
1919
:plural_2 => { :one => 'one' },
@@ -89,3 +89,34 @@ def backend(translations)
8989
backend
9090
end
9191
end
92+
93+
class I18nBackendChainWithKeyValueTest < I18n::TestCase
94+
def setup_backend!(subtrees = true)
95+
first = I18n::Backend::KeyValue.new({}, subtrees)
96+
first.store_translations(:en, :plural_1 => { :one => '%{count}' })
97+
98+
second = I18n::Backend::Simple.new
99+
second.store_translations(:en, :plural_2 => { :one => 'one' })
100+
I18n.backend = I18n::Backend::Chain.new(first, second)
101+
end
102+
103+
test "subtrees enabled: looks up pluralization translations from the first chained backend" do
104+
setup_backend!
105+
assert_equal '1', I18n.t(:plural_1, count: 1)
106+
end
107+
108+
test "subtrees disabled: looks up pluralization translations from the first chained backend" do
109+
setup_backend!(false)
110+
assert_equal '1', I18n.t(:plural_1, count: 1)
111+
end
112+
113+
test "subtrees enabled: looks up translations from the second chained backend" do
114+
setup_backend!
115+
assert_equal 'one', I18n.t(:plural_2, count: 1)
116+
end
117+
118+
test "subtrees disabled: looks up translations from the second chained backend" do
119+
setup_backend!(false)
120+
assert_equal 'one', I18n.t(:plural_2, count: 1)
121+
end
122+
end if I18n::TestCase.key_value?

test/backend/key_value_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ def assert_flattens(expected, nested, escape=true, subtree=true)
4141
end
4242
end
4343

44+
test "subtrees enabled: given incomplete pluralization data it raises I18n::InvalidPluralizationData" do
45+
setup_backend!
46+
store_translations(:en, :bar => { :one => "One" })
47+
assert_raise(I18n::InvalidPluralizationData) { I18n.t(:bar, :count => 2) }
48+
end
49+
50+
test "subtrees disabled: given incomplete pluralization data it returns an error message" do
51+
setup_backend!(false)
52+
store_translations(:en, :bar => { :one => "One" })
53+
assert_equal "translation missing: en.bar", I18n.t(:bar, :count => 2)
54+
end
55+
4456
test "translate handles subtrees for pluralization" do
4557
setup_backend!(false)
4658
store_translations(:en, :bar => { :one => "One" })

0 commit comments

Comments
 (0)