Skip to main content

PluralCategoryUtils

CLDR cardinal plural rules: which form of a word (one, few, many, other, …) a number takes in a language. Nothing here is specific to units; MetricUnitFormatUtils uses it to pick a translated unit name's form.

Types​

PluralCategory​

type PluralCategory = "zero" | "one" | "two" | "few" | "many" | "other"

A CLDR plural category. A translated name has a key for each category its language uses.

Functions​

getPluralCategory​

PluralCategoryUtils.getPluralCategory(
locale: string?,
amount: number,
fraction: string?--

the decimals written, if any

) → PluralCategory

The CLDR plural category of a number as written in locale, from CLDR's cardinal plural rules for every language MetricUnitTranslator ships. amount is the rounded value shown and fraction the decimals written after the decimal mark ("50" for "1.50"), since English writes "1 meter" but "1.0 meters".

  • one only: English, German (i = 1 and v = 0); Spanish and Turkish (n = 1); Vietnamese (i = 0 or n = 1).
  • one and many: French and Portuguese (one for i = 0..1), Italian (one for i = 1 and v = 0); many is a whole multiple of a million, as Spanish also has.
  • one, few, many: Russian and Polish, for whole numbers; their fractions are other.
  • zero, one, two, few, many: Arabic.
  • other only: Chinese, Japanese, Korean, Thai and Indonesian.
PluralCategoryUtils.getPluralCategory("en-us", 1) -- "one"
PluralCategoryUtils.getPluralCategory("en-us", 1, "0") -- "other", as in "1.0 meters"
PluralCategoryUtils.getPluralCategory("fr-fr", 1.5, "5") -- "one"
PluralCategoryUtils.getPluralCategory("ru-ru", 3) -- "few"
PluralCategoryUtils.getPluralCategory("ar", 11) -- "many"
Show raw api
{
    "functions": [
        {
            "name": "getPluralCategory",
            "desc": "The CLDR plural category of a number as written in `locale`, from CLDR's cardinal plural rules for every language\n[MetricUnitTranslator] ships. `amount` is the rounded value shown and `fraction` the decimals written after the\ndecimal mark (`\"50\"` for \"1.50\"), since English writes \"1 meter\" but \"1.0 meters\".\n\n- `one` only: English, German (`i = 1 and v = 0`); Spanish and Turkish (`n = 1`); Vietnamese (`i = 0 or n = 1`).\n- `one` and `many`: French and Portuguese (`one` for `i = 0..1`), Italian (`one` for `i = 1 and v = 0`); `many`\n  is a whole multiple of a million, as Spanish also has.\n- `one`, `few`, `many`: Russian and Polish, for whole numbers; their fractions are `other`.\n- `zero`, `one`, `two`, `few`, `many`: Arabic.\n- `other` only: Chinese, Japanese, Korean, Thai and Indonesian.\n\n```lua\nPluralCategoryUtils.getPluralCategory(\"en-us\", 1) -- \"one\"\nPluralCategoryUtils.getPluralCategory(\"en-us\", 1, \"0\") -- \"other\", as in \"1.0 meters\"\nPluralCategoryUtils.getPluralCategory(\"fr-fr\", 1.5, \"5\") -- \"one\"\nPluralCategoryUtils.getPluralCategory(\"ru-ru\", 3) -- \"few\"\nPluralCategoryUtils.getPluralCategory(\"ar\", 11) -- \"many\"\n```",
            "params": [
                {
                    "name": "locale",
                    "desc": "",
                    "lua_type": "string?"
                },
                {
                    "name": "amount",
                    "desc": "",
                    "lua_type": "number"
                },
                {
                    "name": "fraction",
                    "desc": "the decimals written, if any",
                    "lua_type": "string?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "PluralCategory"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 50,
                "path": "src/metricutils/src/Shared/Localization/PluralCategoryUtils.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "PluralCategory",
            "desc": "A CLDR plural category. A translated name has a key for each category its language uses.",
            "lua_type": "\"zero\" | \"one\" | \"two\" | \"few\" | \"many\" | \"other\"",
            "source": {
                "line": 23,
                "path": "src/metricutils/src/Shared/Localization/PluralCategoryUtils.lua"
            }
        }
    ],
    "name": "PluralCategoryUtils",
    "desc": "CLDR cardinal plural rules: which form of a word (`one`, `few`, `many`, `other`, …) a number takes in a language.\nNothing here is specific to units; [MetricUnitFormatUtils] uses it to pick a translated unit name's form.",
    "source": {
        "line": 8,
        "path": "src/metricutils/src/Shared/Localization/PluralCategoryUtils.lua"
    }
}