https://github.com/catamphetamine/intl-plural-rules-polyfill
A simple and convenient polyfill for `Intl.PluralRules`
https://github.com/catamphetamine/intl-plural-rules-polyfill
Last synced: 4 months ago
JSON representation
A simple and convenient polyfill for `Intl.PluralRules`
- Host: GitHub
- URL: https://github.com/catamphetamine/intl-plural-rules-polyfill
- Owner: catamphetamine
- License: mit
- Created: 2019-07-07T02:29:07.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-07T14:41:47.000Z (almost 6 years ago)
- Last Synced: 2025-02-11T07:36:00.241Z (4 months ago)
- Language: JavaScript
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# intl-plural-rules-polyfill
[](https://www.npmjs.com/package/intl-plural-rules-polyfill)
[](https://www.npmjs.com/package/intl-plural-rules-polyfill)
[](https://www.npmjs.com/package/intl-plural-rules-polyfill)
[](https://coveralls.io/r/catamphetamine/intl-plural-rules-polyfill?branch=master)A simple [`Intl.PluralRules`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules) polyfill. Can be imported as "cardinals"-only. Created as part of [`relative-time-format`](https://github.com/catamphetamine/relative-time-format).
## Install
```
npm install intl-plural-rules-polyfill --save
```If you're not using a bundler then use a [standalone version from a CDN](#cdn).
## Use
```js
import PluralRules from "intl-plural-rules-polyfill"const pluralRules = new PluralRules("en", {
style: "cardinal" // "cardinal" is the default. Other options: "ordinal".
})pluralRules.select(0) // "other"
pluralRules.select(1) // "one"
```The possible return values are:
* `zero`
* `one`
* `two`
* `few`
* `many`
* `other`For more info on which is which read the [official Unicode CLDR documentation](http://cldr.unicode.org/index/cldr-spec/plural-rules). [Unicode CLDR](http://cldr.unicode.org/) (Common Locale Data Repository) is an industry standard and is basically a collection of formatting rules for all locales (date, time, currency, measurement units, numbers, etc).
## Cardinals
If the app only uses `"cardinals"` type and doesn't use `"ordinals"` then the polyfill can be imported as "cardinals"-only.
```js
import PluralRules from "intl-plural-rules-polyfill/cardinal"
```## Under the hood
To determine whether a number is `one`, `few`, or something else, this polyfill uses Unicode CLDR rules for formatting plurals compiled into javascript by [`make-plural`](https://github.com/eemeli/make-plural). These rules are number quantifying functions (one for each locale) which can tell if a number should be treated as `zero`, `one`, `two`, `few`, `many` or `other`. Knowing how these pluralization rules work is not required but anyway here are some links for curious advanced readers: [rules explanation](http://cldr.unicode.org/index/cldr-spec/plural-rules), [list of rules for all locales](http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html), [list of rules for all locales in JSON format](https://github.com/unicode-cldr/cldr-core/blob/master/supplemental/plurals.json) (part of `cldr-core/supplemental` package), [converting those rules to javascript functions](https://github.com/eemeli/make-plural).
Plural rule functions (`./source/ordinals.js` and `./source/cardinals.js`) are generated by running:
```sh
npm install make-plural@latest --save-dev
npm run generate
```## CDN
One can use any npm CDN service, e.g. [unpkg.com](https://unpkg.com) or [jsdelivr.net](https://jsdelivr.net)
```html
new PluralRules('en').select(0)
```
where `[version]` is an npm package version range (for example, `0.2.x` or `^0.2.0`).
## License
[MIT](LICENSE)