Cldr v1.3.2 Cldr.Number.Cardinal View Source

Implements cardinal plural rules for numbers.

Link to this section Summary

Functions

The locales for which plural rules are defined

The configured locales for which plural rules are defined

Returns all the plural rules defined in CLDR

Return the plural rules for a locale

Pluralize a number using plural rules and a substition map

Link to this section Types

Link to this type operand() View Source
operand() :: non_neg_integer()

Link to this section Functions

Link to this function available_locale_names() View Source

The locales for which plural rules are defined

Link to this function known_locale_names() View Source
known_locale_names() :: [Cldr.Locale.locale_name(), ...]

The configured locales for which plural rules are defined.

Returns the intersection of Cldr.known_locale_names/0 and the locales for which Cardinal plural rules are defined.

There are many Cldr locales which don’t have their own plural rules so this list is the intersection of Cldr’s configured locales and those that have rules.

Link to this function plural_rule(number, locale \\ Cldr.get_current_locale(), rounding \\ Math.default_rounding()) View Source
plural_rule(Cldr.Math.number_or_decimal(), Cldr.Locale.locale_name() | Cldr.LanguageTag.t(), atom()) ::
  :zero |
  :one |
  :two |
  :few |
  :many |
  :other

Return the plural key for a given number in a given locale

Returns which plural key (:zero, :one, :two, :few, :many or :other) a given number fits into within the context of a given locale.

Note that these key names should not be interpreted literally. For example, the key returned from Cldr.Number.Ordinal.plural_rule(0, "en") is actually :other, not :zero.

This key can then be used to format a number, date, time, unit, list or other content in a plural-sensitive way.

Examples

iex> Cldr.Number.Cardinal.plural_rule 0, "fr"
:other

iex> Cldr.Number.Cardinal.plural_rule 1, "en"
:one
Link to this function plural_rules() View Source
plural_rules() :: Map.t()

Returns all the plural rules defined in CLDR.

Link to this function plural_rules_for(locale_name) View Source
plural_rules_for(Cldr.Locale.locale_name()) :: [{atom(), list()}, ...]

Return the plural rules for a locale.

The rules are returned in AST form after parsing. This function is primarilty to support Cldr.Gettext.

Link to this function pluralize(number, locale, substitutions) View Source
pluralize(Cldr.Math.number_or_decimal(), Cldr.LanguageTag.t(), %{}) :: any()

Pluralize a number using plural rules and a substition map.

  • number is an integer, float or Decimal

  • locale is any locale returned by Cldr.Locale.new!/1

  • substitutions is a map that maps plural keys to a string. The valid substitution keys are :zero, :one, :two, :few, :many and :other.

See also Cldr.Cardinal.plural_rule/3.

Examples

iex> Cldr.Number.Cardinal.pluralize 1,
...> Locale.new("en"), %{one: "one"}
"one"

iex> Cldr.Number.Cardinal.pluralize 2,
...> Locale.new("en"), %{one: "one"}
nil

iex> Cldr.Number.Cardinal.pluralize 2,
...> Locale.new("en"), %{one: "one", two: "two"}
"two"

iex> Cldr.Number.Cardinal.pluralize 22, Locale.new("en"),
...> %{one: "one", two: "two", other: "other"}
"other"

iex> Cldr.Number.Cardinal.pluralize Decimal.new(1),
...> Locale.new("en"), %{one: "one"}
"one"

iex> Cldr.Number.Cardinal.pluralize Decimal.new(2),
...> Locale.new("en"), %{one: "one"}
nil

iex> Cldr.Number.Cardinal.pluralize Decimal.new(2),
...> Locale.new("en"), %{one: "one", two: "two"}
"two"