Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only require 'other' plural subkey for locales where pluralization rules are undefined #707

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ def resolve(locale, object, subject, options = EMPTY_HASH)

# Picks a translation from a pluralized mnemonic subkey according to English
# pluralization rules :
# - It will pick the :one subkey if count is equal to 1.
# - It will pick the :zero subkey where count is equal to 0 and there
# is a :zero subkey present. This behaviour is not standard with
# regards to the CLDR pluralization rules.
# - It will pick the :one subkey if count is equal to 1 and there is a
# :one subkey present.
# - It will pick the :other subkey otherwise.
# - It will pick the :zero subkey in the special case where count is
# equal to 0 and there is a :zero subkey present. This behaviour is
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether this "not standard" comment means the behaviour is not standard in relation to the CLDR English pluralization rules (because there is a zero subkey) or if there is some sort "generic/unknown locale" rule in CLDR that it's deviating from in some way. If I could get an answer on that I could update the comment to be clearer.

# not standard with regards to the CLDR pluralization rules.
# 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)
Expand Down Expand Up @@ -306,8 +307,9 @@ def translate_localization_format(locale, object, format, options)
end

def pluralization_key(entry, count)
key = :zero if count == 0 && entry.has_key?(:zero)
key ||= count == 1 ? :one : :other
return :zero if count == 0 && entry.has_key?(:zero)
return :one if count == 1 && entry.has_key?(:one)
:other
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/backend/pluralization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,15 @@ def setup
assert_equal "I have 1 Porsche 🚗", I18n.t(:'automobiles.porsche', count: 1, :locale => :xx)
assert_equal "I have 20 Porsches 🚗", I18n.t(:'automobiles.porsche', count: 20, :locale => :xx)
end

test "only 'other' is required on a locale without pluralization rules" do
store_translations(:no_plural_rule_locale,
:stars => {
other: "%{count} stars",
}
)
assert_equal "0 stars", I18n.t('stars', count: 0, :locale => :no_plural_rule_locale)
assert_equal "1 stars", I18n.t('stars', count: 1, :locale => :no_plural_rule_locale)
assert_equal "20 stars", I18n.t('stars', count: 20, :locale => :no_plural_rule_locale)
end
end