Localize.Number.Rbnf (Localize v1.11.0)

Copy Markdown View Source

Rules-Based Number Formatting (RBNF) for algorithmic number systems and spellout forms.

RBNF provides formatting for number systems that don't have simple digit-to-digit mappings, such as Roman numerals, Hebrew numerals, Chinese numerals, and spellout forms like "one hundred twenty-three".

RBNF rules are loaded from locale data at runtime and interpreted by an internal rule processor. Parsed rule ASTs are cached in :persistent_term for performance.

Where a rule set is looked for

A rule set is looked for in the locale's own data, then in each parent's in turn, and last in the root locale und, whose rules write Roman, Hebrew and other numerals. A locale without a spellout rule set gets an Localize.UnknownRbnfRuleError rather than root's digit stubs, so a number is never spelled in another language's words.

CLDR keeps some locales' rules in a parent: Norwegian's spellout rules are in no, the parent of nb and nn, and en-GB has none of its own and takes English's from en. A parent's rules are reached only when the parent is a supported locale, since only a supported locale's data is installed. An application whose supported_locales names nb or en-GB lists no or en as well to spell numbers out in them. Until it does, the Localize.UnknownRbnfRuleError names that parent in its :rules_in field and its message.

Summary

Functions

Returns the available RBNF rule names for a locale.

Formats a number using RBNF rules.

Functions

rule_names_for_locale(locale)

@spec rule_names_for_locale(Localize.locale() | String.t()) ::
  {:ok, [String.t()]} | {:error, Exception.t()}

Returns the available RBNF rule names for a locale.

Arguments

  • locale is a locale identifier atom or string.

Returns

  • {:ok, rule_names} where rule_names is a sorted list of strings.

  • {:error, exception} if RBNF data is not available.

Examples

iex> {:ok, names} = Localize.Number.Rbnf.rule_names_for_locale(:en)
iex> "spellout_cardinal" in names
true

iex> {:ok, names} = Localize.Number.Rbnf.rule_names_for_locale(:en)
iex> "digits_ordinal" in names
true

to_string(number, rule_name, options \\ [])

@spec to_string(number() | Decimal.t(), atom() | String.t(), Keyword.t()) ::
  {:ok, String.t()} | {:error, Exception.t()}

Formats a number using RBNF rules.

Arguments

  • number is an integer or float.

  • rule_name is the rule set name atom or string (e.g., :spellout_cardinal, "roman-upper"), or a family: :ordinal (digits ordinals where the locale has them, else spelled-out ordinals), :spellout (spellout_numbering, else the cardinals), or one family's own name, :digits_ordinal, :spellout_ordinal, :spellout_cardinal or :spellout_numbering. A family resolves to the rule set whose grammatical features agree with the options below. See "Choosing a rule set by grammatical features".

  • options is a keyword list of options.

Options

  • :locale is a locale identifier. The default is :en.

  • :grammatical_gender is the gender the number agrees with: :masculine, :feminine, :neuter, :common, or CLDR's masculine animacy genders :animate, :inanimate and :personal. The default is the family's unmarked rule set where it has one, else the masculine.

  • :grammatical_case is the case, such as :nominative, :genitive, :dative or :accusative. The default is :nominative.

  • :grammatical_number is :singular, :plural or :dual. The default is :singular.

  • :grammatical_definiteness is :indefinite, :definite, :construct or :unspecified. The default is :indefinite.

Each grammatical option takes an atom or its name as a string, and applies only to a family; with any other rule name it is an error. A feature the locale's family does not vary on is ignored, so grammatical_gender: :feminine with locale: :en gives "3rd".

Choosing a rule set by grammatical features

CLDR names gendered and declined rule sets by their features, as in digits_ordinal_masculine_genitive. With no grammatical option a family takes its unmarked rule set (digits_ordinal), as CLDR intends. Otherwise, or where it has none, the rule set agreeing with every requested feature, and marking the most of them, is chosen; with no gender requested the masculine is preferred. A requested value the locale's family does not have is an error.

Returns

  • {:ok, formatted_string} on success.

  • {:error, exception} if the number, rule name or options are not valid, the rules are not available, or no rule set of the family agrees with the grammatical options.

Examples

iex> Localize.Number.Rbnf.to_string(123, :spellout_cardinal, locale: :en)
{:ok, "one hundred twenty-three"}

iex> Localize.Number.Rbnf.to_string(1, :spellout, locale: :en)
{:ok, "one"}

iex> Localize.Number.Rbnf.to_string(1, :ordinal, locale: :en)
{:ok, "1st"}

iex> Localize.Number.Rbnf.to_string(3, :ordinal, locale: :ru, grammatical_gender: :masculine)
{:ok, "3-й"}

iex> Localize.Number.Rbnf.to_string(3, :ordinal, locale: :pl)
{:ok, "trzeci"}