Pluralization Rules
Pluralization is the process of forming different versions of a word to reflect quantity. While English has a simple system (one vs. other, e.g., "1 car" vs. "2 cars"), many languages have more complex rules.
The i18n
gem supports locale-specific pluralization rules, and rails-i18n
provides these rules for a wide variety of languages.
How It Works
When you use a translation helper with a :count
option, Rails uses the pluralization rule defined for the current locale to pick the correct translation key.
I18n.t('cars_count', count: 1) # => uses the :one key
I18n.t('cars_count', count: 2) # => uses the :other key in English, but might use :few in other languages
rails-i18n
provides the backend logic for this by loading a Ruby file from the rails/pluralization/
directory that defines the rule for each supported locale.
Supported Pluralization Rules
This gem implements a variety of pluralization systems based on the Unicode CLDR Plural Rules.
Here are some of the rule sets provided:
- One / Other: The most common rule, used by English, German, Spanish, and many others. A singular form for
1
and a plural form for everything else. - One / Few / Other: Used by languages like Bosnian, Croatian, and Serbian. Has a special form for numbers ending in 2, 3, or 4 (but not 12, 13, 14).
- East Slavic (one, few, many, other): Used by Russian, Ukrainian, and Belarusian. Has four forms for different numerical endings.
- West Slavic (one, few, other): Used by Czech and Slovak. Has three forms (1, 2-4, and other).
- Polish (one, few, many, other): A unique four-form system with complex rules.
- Arabic (zero, one, two, few, many, other): The most complex system with six distinct forms based on the number and its modulo of 100.
- Other only: For languages like Japanese, Chinese, and Korean, where the noun form does not change with quantity.
By including this gem, these rules are automatically configured for your Rails application, allowing you to create accurate pluralized translations without any extra setup.