Class Pluralization

This class enables registering different strategies for pluralization, as well as getting a pluralized translation.

The default pluralization strategy is based on three counters:

  • one: returned when count is equal to absolute 1.
  • zero: returned when count is equal to 0. If this translation is not set, then it defaults to other.
  • other: returned when count is different than absolute 1.

When calling i18n.translate (or its alias), pluralization rules will be triggered whenever the translation options contain count.

Example

A JSON describing the pluralization rules.

{
"en": {
"inbox": {
"zero": "You have no messages",
"one": "You have one message",
"other": "You have %{count} messages"
}
}
}

Example

How to get pluralized translations.

i18n.t("inbox", {count: 0}); // returns "You have no messages"
i18n.t("inbox", {count: 1}); // returns "You have on message"
i18n.t("inbox", {count: 2}); // returns "You have 2 messages"

Hierarchy

  • Pluralization

Constructors

Methods

Constructors

Methods

  • Returns a list of possible pluralization keys. This is defined by a chain of pluralizers, going from locale set explicitly, then the locale set through i18n.locale, defaulting to defaultPluralizer.

    Returns

    The pluralizer function.

    Parameters

    • locale: string

      The locale.

    Returns Pluralizer

  • Register a new pluralizer strategy.

    You may want to support different pluralization rules based on the locales you have to support. If you do, make sure you submit a pull request at https://github.com/fnando/i18n so we can distribute it. For now only Russian is distributed.

    The pluralizer will receive the I18n instance, together with count. Is up to the pluralizer to return a list of possible keys given that count. The Russian pluralizer, for instance, returns one, few, many, other as possible pluralization keys.

    You can view a complete list of pluralization rules at unicode.org.

    You can also leverage make-plural, rather than writing all your pluralization functions. For this, you must wrap make-plural's function by using useMakePlural({ pluralizer, includeZero, ordinal }):

    Example

    import { ru } from "make-plural";
    import { useMakePlural } from "i18n-js";

    i18n.pluralization.register("ru", useMakePlural({ pluralizer: ru }));

    Returns

    Parameters

    • locale: string

      The locale.

    • pluralizer: Pluralizer

      The pluralizer function.

    Returns void

Generated using TypeDoc