GeezerVison is showing github.com — Merge pull request #407 from fatkodima/fix-key-value-subtrees · ruby-i18n/i18n@ba8b206
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/i18n/backend/base.rb +19-9Lines changed: 19 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,17 +34,21 @@ def translate(locale, key, options = {}) | 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 @@ def lookup(locale, key, scope = [], options = {}) | 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 @@ def resolve(locale, object, subject, options = {}) | 145 | 153 | def pluralize(locale, entry, count) | 146 | 154 | return entry unless entry.is_a?(Hash) && count | 147 | 155 |
| 148 | | - key = :zero if count == 0 && entry.has_key?(:zero) | 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 @@ def pluralize(locale, entry, count) | 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 @@ def translate_localization_format(locale, object, format, options) | 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 |
Collapse file Copy file name to clipboardExpand all lines: lib/i18n/backend/key_value.rb +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,6 +103,10 @@ def available_locales | 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 @@ def lookup(locale, key, scope = [], options = {}) | 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 @@ def has_key?(key) | 132 | 145 | def [](key) | 133 | 146 | unless @subtree && value = @subtree[key] | 134 | 147 | 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 | 136 | 152 | end | 137 | 153 | value | 138 | 154 | end | |
Collapse file Copy file name to clipboardExpand all lines: test/backend/chain_test.rb +32-1Lines changed: 32 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,7 +13,7 @@ def setup | 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 @@ def backend(translations) | 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? |
Collapse file Copy file name to clipboardExpand all lines: test/backend/key_value_test.rb +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,18 @@ def assert_flattens(expected, nested, escape=true, subtree=true) | 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" }) | |
|