What is make-plural?
The make-plural npm package is a utility for handling pluralization rules in various languages. It allows developers to determine the correct plural form of a word based on a given number, which is particularly useful for internationalization (i18n) and localization (l10n) tasks.
What are make-plural's main functionalities?
Determine Plural Form
This feature allows you to determine the plural form of a word in a specified language based on a given number. In this example, the plural form for English ('en') is determined for the numbers 1 and 2.
const makePlural = require('make-plural');
const plural = makePlural('en');
console.log(plural(1)); // 'one'
console.log(plural(2)); // 'other'
Support for Multiple Languages
This feature demonstrates the package's support for multiple languages. It shows how to determine the plural form for both English and French.
const makePlural = require('make-plural');
const pluralEn = makePlural('en');
const pluralFr = makePlural('fr');
console.log(pluralEn(1)); // 'one'
console.log(pluralEn(2)); // 'other'
console.log(pluralFr(1)); // 'one'
console.log(pluralFr(2)); // 'other'
Custom Plural Rules
This feature allows you to define custom pluralization rules. In this example, custom rules are provided to determine the plural form.
const makePlural = require('make-plural');
const customRules = {
one: 'n == 1',
other: 'true'
};
const pluralCustom = makePlural(customRules);
console.log(pluralCustom(1)); // 'one'
console.log(pluralCustom(2)); // 'other'
Other packages similar to make-plural
pluralize
The pluralize package provides simple pluralization and singularization of English words. It is less comprehensive than make-plural as it primarily focuses on English and does not support custom rules or multiple languages.
i18next
i18next is a powerful internationalization framework that supports pluralization among many other features. It is more comprehensive than make-plural, offering a full suite of i18n tools, but it may be overkill if you only need pluralization.
messageformat
The messageformat package provides support for handling complex message formatting, including pluralization, gender, and other language-specific rules. It is more feature-rich compared to make-plural, but also more complex to use.
make-plural
make-plural
provides JavaScript functions determining the pluralization categories of the approximately 200 languages included in the Unicode CLDR.
In addition to the more commonly considered cardinal plurals (e.g. one book, two books), it also support ordinal plurals (e.g. 1st book, 2nd book, etc).
It's used internally by the intl-pluralrules polyfill.
The categorization functions are pre-compiled, require no runtime dependencies, and should compress to about 2.5kB.
The ES module exports in particular are designed to work well with tree-shaking, allowing for further size savings.
In order to generate an even smaller file from a subset of all possible language or to otherwise customise the modules, use make-plural-cli or make-plural-compiler.
Installation & Usage
npm install make-plural
import * as Plurals from 'make-plural/plurals'
import * as Cardinals from 'make-plural/cardinals'
import * as Examples from 'make-plural/examples'
import * as Ordinals from 'make-plural/ordinals'
import * as Categories from 'make-plural/pluralCategories'
import * as PluralRanges from 'make-plural/ranges'
Each of the endpoints is available with both UMD (.js) and ES (.mjs) packaging.
Cardinals
, Ordinals
and Plurals
each export a set of functions keyed by locale code,
returning the pluralization category for the input (either a number or a string representation of a number).
Plurals
functions also accept a second boolean parameter to return
the ordinal (true
) rather than cardinal (false
, default) plural category.
Note that Ordinals
includes a slightly smaller subset of locales than Cardinals
and Plurals
,
due to a lack of data in the CLDR.PluralRanges
provides a set of functions similarly keyed by locale code,
but returning the pliralization category of a numerical range,
given the corresponding categories of its start and end values as arguments.Categories
has a similar structure,
but contains for each language an array of the pluralization categories
the cardinal and ordinal rules that that language's pluralization function may output.Examples
provide sample numeric values for each language's categories.
The object keys are named using the corresponding 2-3 character language code.
Due to JavaScript identifier restrictions, there are two exceptions:
Portugese as spoken in Portugal (pt-PT
; pt
is Brazilian Portuguese) is available as pt_PT
, and the now-deprecated in
subtag for Indonesian (preferred: id
) is available as _in
.
The transformation used for these names is available as safe-identifier on npm.
The package file paths and exports are structured in a manner that should allow transparent usage in any module system.
In particular, when importing as an ES6 module, tree shaking should be able drop all but the explicitly used functions from the output, provided that named rather than wildcard imports are used.
import { en } from 'make-plural'
en(1)
en('1.0')
en(2)
en(2, true)
String(en)
import { en as ordinalEn } from 'make-plural/ordinals'
ordinalEn(3)
import * as Categories from 'make-plural/pluralCategories'
import { en as rangeEn, ro as rangeRo } from 'make-plural/ranges'
String(rangeEn)
String(rangeRo)