Skip to content

Commit

Permalink
Merge pull request ruby-i18n#503 from CrAsH1101/preserve-count-option
Browse files Browse the repository at this point in the history
Preserve count option
  • Loading branch information
radar authored Jan 7, 2020
2 parents 30292b1 + 00b6489 commit b7f69f7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def resolve(locale, object, subject, options = EMPTY_HASH)
result = catch(:exception) do
case subject
when Symbol
I18n.translate(subject, **options.merge(:locale => locale, :throw => true).except(:count))
I18n.translate(subject, **options.merge(:locale => locale, :throw => true))
when Proc
date_or_time = options.delete(:object) || object
resolve(locale, object, subject.call(date_or_time, options))
Expand All @@ -163,7 +163,7 @@ def resolve(locale, object, subject, options = EMPTY_HASH)
# not standard with regards to the CLDR pluralization rules.
# Other backends can implement more flexible or complex pluralization rules.
def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) && count
return entry unless entry.is_a?(Hash) && count && entry.values.none? { |v| v.is_a?(Hash) }

key = pluralization_key(entry, count)
raise InvalidPluralizationData.new(entry, count, key) unless entry.has_key?(key)
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/backend/pluralization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Pluralization
# either pick a special :zero translation even for languages where the
# pluralizer does not return a :zero key.
def pluralize(locale, entry, count)
return entry unless entry.is_a?(Hash) and count
return entry unless entry.is_a?(Hash) && count && entry.values.none? { |v| v.is_a?(Hash) }

pluralizer = pluralizer(locale)
if pluralizer.respond_to?(:call)
Expand Down
6 changes: 5 additions & 1 deletion test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Backend < I18n::Backend::Simple
def setup
super
I18n.backend = Backend.new
store_translations(:en, :foo => 'Foo in :en', :bar => 'Bar in :en', :buz => 'Buz in :en', :interpolate => 'Interpolate %{value}')
store_translations(:en, :foo => 'Foo in :en', :bar => 'Bar in :en', :buz => 'Buz in :en', :interpolate => 'Interpolate %{value}', :interpolate_count => 'Interpolate %{value} %{count}')
store_translations(:de, :bar => 'Bar in :de', :baz => 'Baz in :de')
store_translations(:'de-DE', :baz => 'Baz in :de-DE')
store_translations(:'pt-BR', :baz => 'Baz in :pt-BR')
Expand All @@ -28,6 +28,10 @@ def setup
assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE')
end

test "keeps the count option when defaulting to a different key" do
assert_equal 'Interpolate 5 10', I18n.t(:non_existant, default: :interpolate_count, count: 10, value: 5)
end

test "returns the :de translation for a missing :'de-DE' when :default is a String" do
assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => "Default Bar")
assert_equal "Default Bar", I18n.t(:missing_bar, :locale => :'de-DE', :default => "Default Bar")
Expand Down

0 comments on commit b7f69f7

Please sign in to comment.