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.js
A JavaScript module that translates Unicode CLDR pluralization rules to
executable JavaScript.
Installation
Using npm:
npm install make-plural
Usage
> plurals = require('make-plural')
{ set_rules: [Function], build: [Function] }
> console.log( plurals.build('fr') )
function(n) {
if (n >= 0 && n < 2) return 'one';
return 'other';
}
> console.log( plurals.build('sk') )
function(n) {
var s = String(n).split('.'), i = s[0], v0 = !s[1];
if (n == 1 && v0) return 'one';
if ((i >= 2 && i <= 4) && v0) return 'few';
if (!v0) return 'many';
return 'other';
}
> sk = plurals.build('sk', true)
[Function]
> sk(1)
'one'
> sk(3.0)
'few'
> sk('1.0')
'many'
> sk('0')
'other'
Methods
build(lc, op_function)
By default, returns a string representation of a function that takes a single
argument n
and returns its plural category for the given locale lc
. If
op_function
is true
, it returns an executable function instead.
set_rules(cldr)
Sets the used CLDR rules to cldr
, which may be an object or the path to a
JSON file formatted like this.
By default, the included rules in data/unicode-cldr-plural-rules.json
are
used.
Dependencies
None.