gettext v0.13.1 Gettext.Plural behaviour

Behaviour and default implementation for finding plural forms in given locales.

This module both defines the Gettext.Plural behaviour and provides a default implementation for it.

Plural forms

For a given language, there is a grammatical rule on how to change words depending on the number qualifying the word. Different languages can have different rules. [source]

Such grammatical rules define a number of plural forms. For example, English has two plural forms: one for when there is just one element (the singular) and another one for when there are zero or more than one elements (the plural). There are languages which only have one plural form and there are languages which have more than two.

In GNU Gettext (and in Gettext for Elixir), plural forms are represented by increasing 0-indexed integers. For example, in English 0 means singular and 1 means plural.

The goal of this module is to determine, given a locale:

  • how many plural forms exist in that locale (nplurals/1);
  • to what plural form a given number of elements belongs to in that locale (plural/2).

Default implementation

Gettext.Plural provides a default implementation of a plural module. Most languages used on Earth should be covered by this default implementation. If custom pluralization rules are needed (for example, to add additional languages) a different plural module can be specified when creating a Gettext backend. For example, pluralization rules for the Elvish language could be added as follows:

defmodule MyApp.Plural do
  @behaviour Gettext.Plural

  def nplurals("elv"), do: 3

  def plural("elv", 0), do: 0
  def plural("elv", 1), do: 1
  def plural("elv", _), do: 2

  # Fallback to Gettext.Plural
  def nplurals(locale), do: Gettext.Plural.nplurals(locale)
  def plural(locale, n), do: Gettext.Plural.plural(locale, n)

defmodule MyApp.Gettext do
  use Gettext, otp_app: :my_app, plural_forms: MyApp.Plural

The mathematical expressions used in this module to determine the plural form of a given number of elements are taken from this page as well as from Mozilla’s guide on “Localization and plurals”.

Unknown locales

Trying to call Gettext.Plural functions with unknown locales will result in a UnknownLocaleError exception.

Language and country

Often, a locale is composed as a language and country couple, such as en_US. The default implementation for Gettext.Plural handles xx_YY by forwarding it to xx (except for just Brazilian Portuguese, pt_BR, which is not forwarded to pt as pluralization rules slightly differ). We treat the underscore as a separator according to ISO 15897. Sometimes, a dash - is used as a separator (e.g., en-US): this is not forwarded to en in the default Gettext.Plural (and it will raise an Gettext.Plural.UnknownLocaleError exception).


An example of the plural form of a given number of elements in the Polish language:

iex> Plural.plural("pl", 1)
iex> Plural.plural("pl", 2)
iex> Plural.plural("pl", 5)
iex> Plural.plural("pl", 112)

As expected, nplurals/1 returns the possible number of plural forms:

iex> Plural.nplurals("pl")



Returns the number of possible plural forms in the given locale

Returns the plural form in the given locale for the given count of elements


plural(locale, count)


nplurals(locale :: String.t) :: pos_integer

Returns the number of possible plural forms in the given locale.

plural(locale, count)
plural(locale :: String.t, count :: integer) :: plural_form :: non_neg_integer

Returns the plural form in the given locale for the given count of elements.