Localize.Number.PluralRule.Range (Localize v1.0.0-rc.2)

Copy Markdown View Source

Plural category selection for numeric ranges.

Implements the TR35 plural ranges rules: the plural category of a range like "1–2 days" is derived from the categories of its endpoints using per-language range rules from the CLDR supplemental data. The primary functions are plural_rule/3 for endpoint categories and plural_rule_for/3 for endpoint numbers.

Summary

Functions

Returns the plural category for a range given the categories of its endpoints.

Returns the plural category for a range given the numbers of its endpoints.

Functions

plural_rule(start_category, end_category, locale)

@spec plural_rule(atom(), atom(), atom() | String.t() | Localize.LanguageTag.t()) ::
  {:ok, atom()} | {:error, Exception.t()}

Returns the plural category for a range given the categories of its endpoints.

Arguments

  • start_category is the plural category of the range start.

  • end_category is the plural category of the range end.

  • locale is a locale identifier atom, string, or a Localize.LanguageTag.t/0 struct.

Returns

  • {:ok, category} where category is one of :zero, :one, :two, :few, :many, or :other.

  • {:error, exception} if the locale is invalid.

Examples

iex> Localize.Number.PluralRule.Range.plural_rule(:one, :other, "fr")
{:ok, :other}

iex> Localize.Number.PluralRule.Range.plural_rule(:one, :one, "fr")
{:ok, :one}

plural_rule_for(start_number, end_number, locale)

@spec plural_rule_for(
  number() | Decimal.t(),
  number() | Decimal.t(),
  atom() | String.t() | Localize.LanguageTag.t()
) :: {:ok, atom()} | {:error, Exception.t()}

Returns the plural category for a range given the numbers of its endpoints.

The endpoint categories are selected with the locale's cardinal plural rules, then combined with plural_rule/3.

Arguments

  • start_number is the start of the range (integer, float, or Decimal).

  • end_number is the end of the range.

  • locale is a locale identifier atom, string, or a Localize.LanguageTag.t/0 struct.

Returns

  • {:ok, category} where category is one of :zero, :one, :two, :few, :many, or :other.

  • {:error, exception} if the locale is invalid.

Examples

iex> Localize.Number.PluralRule.Range.plural_rule_for(0, 1, "fr")
{:ok, :one}

iex> Localize.Number.PluralRule.Range.plural_rule_for(1, 2, "fr")
{:ok, :other}