API

Plurals API

Package

@foo-i18n/plurals

Because plural rules different across different locales, this module’s purpose is to offer standardized plurality values with rules generated from CLDR data.

Declarations

type Plurality = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';

type PluralRule = {
  ordinal(n: number): Plurality;
  cardinal(n: number): Plurality;
};

Usage

import { en } from '@foo-i18n/plurals';
// import en from '@foo-i18n/plurals/en';

// 1, 2, 3, ...
console.log(en.cardinal(1));
// "one";
console.log(en.cardinal(0.3));
// "other"

// 1st, 2nd, 3rd, ...
console.log(en.ordinal(4));
// "other"

It is recommended to set "moduleResolution": "NodeNext", in tsconfig.json to enable subpath exports for this package. Otherwise, import paths may differ from the example above.