Idiom.Plural (idiom v0.6.1)

Functionality for handling plurals and plural suffixes.

Idiom handles plurals by adding suffixes to keys. These suffixes are as defined by the Unicode CLDR plural rules:

  • zero
  • one
  • two
  • few
  • many
  • other

Used suffixes differ greatly by language. In order to support them all, and also make this module easier to keep up-to-date, the PluralAST module parses them and generates ASTs for cond expressions. This module reads the definition file at compile time, and generates helper functions for each language.

Summary

Functions

Returns the appropriate plural suffix based on a given locale, count and plural type.

Types

@type count() :: binary() | float() | integer() | Decimal.t()

Functions

Link to this function

get_suffix(locale, count, opts \\ [])

@spec get_suffix(String.t(), count(), [{:type, :cardinal | :ordinal}]) :: String.t()

Returns the appropriate plural suffix based on a given locale, count and plural type.

The function will determine the correct plural form to use based on the count parameter. It supports different types of count values, including binary, float, integer and Decimal types.

It also allows selecting whether you want to receive the suffix for cardinal or ordinal plurals by passing :cardinal or :ordinal as the type option, where cardinal is the default plural type.

When the count is nil, the function will default to other.

Examples

iex> Idiom.Plural.get_suffix("en", 1)
"one"

iex> Idiom.Plural.get_suffix("en", 2, type: :cardinal)
"other"

iex> Idiom.Plural.get_suffix("en", 2, type: :ordinal)
"two"

iex> Idiom.Plural.get_suffix("ar", 0)
"zero"

iex> Idiom.Plural.get_suffix("ar", "5")
"few"

iex> Idiom.Plural.get_suffix("ar", Decimal.new(5))
"few"