Skip to content

Commit

Permalink
Merge pull request #637 from movermeyer/movermeyer/nested_pluralizati…
Browse files Browse the repository at this point in the history
…on_with_base_backend

Revert #503 changes for `Backend::Base`
  • Loading branch information
radar committed Feb 3, 2023
2 parents c635958 + cda0ea5 commit 42052e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def resolve(locale, object, subject, options = EMPTY_HASH)
# Other backends can implement more flexible or complex pluralization rules.
def pluralize(locale, entry, count)
entry = entry.reject { |k, _v| k == :attributes } if entry.is_a?(Hash)
return entry unless entry.is_a?(Hash) && count && entry.values.none? { |v| v.is_a?(Hash) }
return entry unless entry.is_a?(Hash) && count

key = pluralization_key(entry, count)
raise InvalidPluralizationData.new(entry, count, key) unless entry.has_key?(key)
Expand Down
17 changes: 17 additions & 0 deletions test/backend/pluralization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ def setup
assert_equal 'one', I18n.t(:count => 1, :default => @entry, :locale => :pirate)
end

test "Nested keys within pluralization context" do
store_translations(:xx,
:stars => {
one: "%{count} star",
other: "%{count} stars",
special: {
one: "%{count} special star",
other: "%{count} special stars",
}
}
)
assert_equal "1 star", I18n.t('stars', count: 1, :locale => :xx)
assert_equal "20 stars", I18n.t('stars', count: 20, :locale => :xx)
assert_equal "1 special star", I18n.t('stars.special', count: 1, :locale => :xx)
assert_equal "20 special stars", I18n.t('stars.special', count: 20, :locale => :xx)
end

test "Fallbacks can pick up rules from fallback locales, too" do
assert_equal @rule, I18n.backend.send(:pluralizer, :'xx-XX')
end
Expand Down
17 changes: 17 additions & 0 deletions test/backend/simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ def setup
assert_equal true, I18n.backend.initialized?
end

test "Nested keys within pluralization context" do
store_translations(:en,
:stars => {
one: "%{count} star",
other: "%{count} stars",
special: {
one: "%{count} special star",
other: "%{count} special stars",
}
}
)
assert_equal "1 star", I18n.t('stars', count: 1, :locale => :en)
assert_equal "20 stars", I18n.t('stars', count: 20, :locale => :en)
assert_equal "1 special star", I18n.t('stars.special', count: 1, :locale => :en)
assert_equal "20 special stars", I18n.t('stars.special', count: 20, :locale => :en)
end

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

0 comments on commit 42052e8

Please sign in to comment.