# Pluralization rules

# Built-in rules

Crystal I18n has built-in support for most of the existing pluralization rules. All these pluralizations rules are defined and implemented under the I18n::Pluralization::Rule namespace.

# Custom rules

It is possible to define custom pluralization rules by subclassing the I18n::Pluralization::Rule abstract class. Subclasses must implement an #apply method that takes an single count argument (float or int) and that returns a valid CLDR plural category tag (opens new window). Some of these tags include :zero, :one, :two, :few, :many and :other.

Here is an example pluralization rule that could be written for the English language:

class EnglishRule < Rule
  def apply(count : Float | Int) : Symbol
    count == 1 ? :one : :other
  end
end

Once implemented, custom pluralization rules have to be "registered" to Crystal I18n by using the I18n::Pluralization#register_rule method. For example the above pluralization rule could be registered to Crystal I18n as follows:

I18n::Pluralization.register_rule(:en, EnglishRule)

Then, every time pluralized translations need to be generated for the en locale, the registered pluralization rule will be used automatically by Crystal I18n.