Skip to content

Commit

Permalink
Merge pull request #419 from wjordan/key_value_pluralization_fix
Browse files Browse the repository at this point in the history
Fix pluralization behavior for KeyValue backend with subtrees disabled
  • Loading branch information
radar authored Jul 15, 2018
2 parents 697b42d + d966cf1 commit 6870a98
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/i18n/backend/key_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def pluralize(locale, entry, count)
if subtrees?
super
else
return entry unless entry.is_a?(Hash)
key = pluralization_key(entry, count)
entry[key]
end
Expand Down
5 changes: 5 additions & 0 deletions test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ def setup
I18n.enforce_available_locales = false
end
end

test "returns fallback default given missing pluralization data" do
assert_equal 'default', I18n.t(:missing_bar, count: 1, default: 'default')
assert_equal 'default', I18n.t(:missing_bar, count: 0, default: 'default')
end
end

class I18nBackendFallbacksLocalizeTest < I18n::TestCase
Expand Down
24 changes: 24 additions & 0 deletions test/backend/key_value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,28 @@ def assert_flattens(expected, nested, escape=true, subtree=true)
store_translations(:en, :bar => { :one => "One" })
assert_equal("One", I18n.t("bar", :count => 1))
end

test "subtrees enabled: returns localized string given missing pluralization data" do
setup_backend!(true)
assert_equal 'bar', I18n.t("foo.bar", count: 1)
end

test "subtrees disabled: returns localized string given missing pluralization data" do
setup_backend!(false)
assert_equal 'bar', I18n.t("foo.bar", count: 1)
end

test "subtrees enabled: Returns fallback default given missing pluralization data" do
setup_backend!(true)
I18n.backend.extend I18n::Backend::Fallbacks
assert_equal 'default', I18n.t(:missing_bar, count: 1, default: 'default')
assert_equal 'default', I18n.t(:missing_bar, count: 0, default: 'default')
end

test "subtrees disabled: Returns fallback default given missing pluralization data" do
setup_backend!(false)
I18n.backend.extend I18n::Backend::Fallbacks
assert_equal 'default', I18n.t(:missing_bar, count: 1, default: 'default')
assert_equal 'default', I18n.t(:missing_bar, count: 0, default: 'default')
end
end if I18n::TestCase.key_value?
4 changes: 4 additions & 0 deletions test/backend/simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ def setup
I18n.backend.reload!
assert_equal false, I18n.backend.initialized?
end

test "returns localized string given missing pluralization data" do
assert_equal 'baz', I18n.t('foo.bar', count: 1)
end
end

0 comments on commit 6870a98

Please sign in to comment.