i18n 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/i18n/backend/base.rb +19 -9
- data/lib/i18n/backend/key_value.rb +17 -1
- data/lib/i18n/backend/simple.rb +1 -1
- data/lib/i18n/version.rb +1 -1
- data/test/backend/chain_test.rb +32 -1
- data/test/backend/key_value_test.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c2f427a87de527a30014666816a5281355600edcd9732d48d0e3f5ed3a2892a
|
4
|
+
data.tar.gz: 0c833c66aebca3795c3881be183c58bbe85879f767c70bff65cb76dd75356edb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87c99a7886fbc70684bfa121fa4d6ba36be32d87f6bcc1bc2a804071c8e197f0b6714ff068fda82a982d33cdd7836f7a3566bc4678bc7061f087939031f83458
|
7
|
+
data.tar.gz: 87a4fd3e214d2426f8a97476bbbed0730486f6f5d86dfc47d72f980058a7a367124f659b12180a9a8d524ce9465a2bbd784e8c41d2d0fa1e82a65028b03e1488
|
data/lib/i18n/backend/base.rb
CHANGED
@@ -34,17 +34,21 @@ module I18n
|
|
34
34
|
entry = resolve(locale, key, entry, options)
|
35
35
|
end
|
36
36
|
|
37
|
-
entry = entry.dup if entry.is_a?(String)
|
38
|
-
|
39
37
|
count = options[:count]
|
40
|
-
entry = pluralize(locale, entry, count) if count
|
41
38
|
|
42
|
-
if entry.nil?
|
39
|
+
if entry.nil? && (subtrees? || !count)
|
43
40
|
if (options.key?(:default) && !options[:default].nil?) || !options.key?(:default)
|
44
41
|
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
|
45
42
|
end
|
46
43
|
end
|
47
44
|
|
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
|
+
|
48
52
|
deep_interpolation = options[:deep_interpolation]
|
49
53
|
values = options.except(*RESERVED_KEYS)
|
50
54
|
if values
|
@@ -97,6 +101,10 @@ module I18n
|
|
97
101
|
raise NotImplementedError
|
98
102
|
end
|
99
103
|
|
104
|
+
def subtrees?
|
105
|
+
true
|
106
|
+
end
|
107
|
+
|
100
108
|
# Evaluates defaults.
|
101
109
|
# If given subject is an Array, it walks the array and returns the
|
102
110
|
# first translation that can be resolved. Otherwise it tries to resolve
|
@@ -145,8 +153,7 @@ module I18n
|
|
145
153
|
def pluralize(locale, entry, count)
|
146
154
|
return entry unless entry.is_a?(Hash) && count
|
147
155
|
|
148
|
-
key =
|
149
|
-
key ||= count == 1 ? :one : :other
|
156
|
+
key = pluralization_key(entry, count)
|
150
157
|
raise InvalidPluralizationData.new(entry, count, key) unless entry.has_key?(key)
|
151
158
|
entry[key]
|
152
159
|
end
|
@@ -161,9 +168,7 @@ module I18n
|
|
161
168
|
# each element of the array is recursively interpolated (until it finds a string)
|
162
169
|
# method interpolates ["yes, %{user}", ["maybe no, %{user}, "no, %{user}"]], :user => "bartuz"
|
163
170
|
# # => "["yes, bartuz",["maybe no, bartuz", "no, bartuz"]]"
|
164
|
-
|
165
|
-
|
166
|
-
def interpolate(locale, subject, values = {})
|
171
|
+
def interpolate(locale, subject, values = {})
|
167
172
|
return subject if values.empty?
|
168
173
|
|
169
174
|
case subject
|
@@ -240,6 +245,11 @@ module I18n
|
|
240
245
|
end
|
241
246
|
end
|
242
247
|
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
|
243
253
|
end
|
244
254
|
end
|
245
255
|
end
|
@@ -103,6 +103,10 @@ module I18n
|
|
103
103
|
|
104
104
|
protected
|
105
105
|
|
106
|
+
def subtrees?
|
107
|
+
@subtrees
|
108
|
+
end
|
109
|
+
|
106
110
|
def lookup(locale, key, scope = [], options = {})
|
107
111
|
key = normalize_flat_keys(locale, key, scope, options[:separator])
|
108
112
|
value = @store["#{locale}.#{key}"]
|
@@ -116,6 +120,15 @@ module I18n
|
|
116
120
|
SubtreeProxy.new("#{locale}.#{key}", @store)
|
117
121
|
end
|
118
122
|
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
|
119
132
|
end
|
120
133
|
|
121
134
|
class SubtreeProxy
|
@@ -132,7 +145,10 @@ module I18n
|
|
132
145
|
def [](key)
|
133
146
|
unless @subtree && value = @subtree[key]
|
134
147
|
value = @store["#{@master_key}.#{key}"]
|
135
|
-
|
148
|
+
if value
|
149
|
+
value = JSON.decode(value)
|
150
|
+
(@subtree ||= {})[key] = value
|
151
|
+
end
|
136
152
|
end
|
137
153
|
value
|
138
154
|
end
|
data/lib/i18n/backend/simple.rb
CHANGED
@@ -44,7 +44,7 @@ module I18n
|
|
44
44
|
def available_locales
|
45
45
|
init_translations unless initialized?
|
46
46
|
translations.inject([]) do |locales, (locale, data)|
|
47
|
-
locales << locale unless (data.
|
47
|
+
locales << locale unless data.size <= 1 && (data.empty? || data.has_key?(:i18n))
|
48
48
|
locales
|
49
49
|
end
|
50
50
|
end
|
data/lib/i18n/version.rb
CHANGED
data/test/backend/chain_test.rb
CHANGED
@@ -13,7 +13,7 @@ class I18nBackendChainTest < I18n::TestCase
|
|
13
13
|
})
|
14
14
|
@second = backend(:en => {
|
15
15
|
:bar => 'Bar', :formats => {
|
16
|
-
:long => 'long',
|
16
|
+
:long => 'long',
|
17
17
|
:subformats => {:long => 'long'},
|
18
18
|
},
|
19
19
|
:plural_2 => { :one => 'one' },
|
@@ -89,3 +89,34 @@ class I18nBackendChainTest < I18n::TestCase
|
|
89
89
|
backend
|
90
90
|
end
|
91
91
|
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?
|
@@ -41,6 +41,18 @@ class I18nBackendKeyValueTest < I18n::TestCase
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
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
|
+
|
44
56
|
test "translate handles subtrees for pluralization" do
|
45
57
|
setup_backend!(false)
|
46
58
|
store_translations(:en, :bar => { :one => "One" })
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sven Fuchs
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2018-
|
16
|
+
date: 2018-02-09 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: concurrent-ruby
|