Idiom.Plural (idiom v0.6.8)

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

Summary

Functions

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

Returns a locale's plural suffixes for the specific 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"
Link to this function

get_suffixes(locale, type)

@spec get_suffixes(String.t(), :cardinal | :ordinal) :: String.t()

Returns a locale's plural suffixes for the specific plural type.

Examples

iex> Idiom.Plural.get_suffixes("en-US", :cardinal)
["one", "other"]

iex> Idiom.Plural.get_suffixes("en-US", :ordinal)
["one", "two", "few", "other"]