diff --git a/lib/i18n/backend/base.rb b/lib/i18n/backend/base.rb index 4cbcc3c0..682df9d9 100644 --- a/lib/i18n/backend/base.rb +++ b/lib/i18n/backend/base.rb @@ -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) diff --git a/test/backend/pluralization_test.rb b/test/backend/pluralization_test.rb index 1e61b944..fa319ba4 100644 --- a/test/backend/pluralization_test.rb +++ b/test/backend/pluralization_test.rb @@ -44,6 +44,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 diff --git a/test/backend/simple_test.rb b/test/backend/simple_test.rb index 23de4864..d568961d 100644 --- a/test/backend/simple_test.rb +++ b/test/backend/simple_test.rb @@ -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