plurals-cldr

Plurals support, generated from CLDR.

MIT 7 个版本
安装
npm install plurals-cldr
yarn add plurals-cldr
pnpm add plurals-cldr
bun add plurals-cldr
README

plurals-cldr - plurals support for JS

CI NPM version Coverage Status

Key benefits:

  • Competely automated code generation from CLDR on update.
  • Generated code automatically tested with CLDR fixtures.
  • Both cardinal and ordinal forms support.
  • Rules for all languages are stored in single file in very compact form.

Installation

node.js:

$ npm install plurals-cldr

API

.(locale, number)

Returns form name for given number. Number can be passed as string to keep tailing decimal zeros. If locale not supported, returns null.

var plural = require('plural-cldr');

// Get cardinal form name
//
// Params:
//
// - locale
// - number (Number|String)
//
plural('ru', 0)   // -> 'many'
plural('ru', 1)   // -> 'one'
plural('ru', 2)   // -> 'few'
plural('ru', 19)  // -> 'many'
plural('ru', 0.5) // -> 'other'

.forms(locale)

Returns array of available forms for specified locale. If locale not supported, returns null.

.indexOf(locale, number)

Returns index of form for specified locale. That's convenient, if you wish to implement lookup from compact ordered list, like babelfish does.

If locale not supported, function returns -1.

Order of forms is the same for all languages: zero, one, two, few, many, other. Remove unavailable forms, and you will get indexes of each.

.ordinal(), .ordinal.forms(), ordinal.indexOf()

The same as above, but for ordinal forms.

Update CLDR version (for developers)

You need to bump cldr-core version & run rebuild:

npm install cldr-core@latest --save-dev
rm package-lock.json
rm -rf node_modules
npm install
npm run build

References

  1. CLDR downloads
  2. Latest chart
  3. Syntax description
版本列表
2.0.1 2021-11-30
2.0.0 2021-11-29
1.0.4 2020-05-18
1.0.3 2016-11-03
1.0.2 2016-01-17
1.0.1 2014-12-14
1.0.0 2014-12-14