
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
plural-rules
Advanced tools
Evaluates plural rules, so that localization libraries can choose the right plural form.
Evaluates locale-specific plural rules to identify the right plural form for a cardinal number, which represents an item count. Internationalization libraries can utilize it to choose the right localized string.
If you are looking for a smaller and faster library using Mozilla plural rules, see fast-plural-rules.
import { getPluralFormForCardinal } from 'plural-rules'
// Returns index of the plural form for the specified locale and cardinal.
getPluralFormForCardinal('en', 1) // Returns "one"; "1 file"
getPluralFormForCardinal('en', 2) // Returns "other"; "2 files"
getPluralFormForCardinal('en', 5) // Returns "other"; "5 files"
getPluralFormForCardinal('cs', 1) // Returns "one"; "1 soubor"
getPluralFormForCardinal('cs', 2) // Returns "few"; "2 soubory"
getPluralFormForCardinal('cs', 5) // Returns "other"; "5 souborů"
// Returns a localized message for the specified locale and cardinal.
localizeMessage('en', 'fileCount', 3) // Returns "3 files"
localizeMessage('cs', 'fileCount', 3) // Returns "3 soubory"
// Returns a localized message for the specified locale and cardinal.
function localizeMessage (locale, messageKey, cardinal) {
const pluralForm = getPluralFormForCardinal(locale, cardinal)
const messageFormat = messages[locale][messageKey][pluralForm]
return messageFormat.replace('{0}', cardinal)
}
// Localized messages organized by locales and message keys.
const messages = {
en: {
fileCount: {
one: '{0} file', // singular
other: '{0} files' // plural
}
},
cs: {
fileCount: {
one: '{0} soubor', // singular
few: '{0} soubory', // plural for 2-4 items
other: '{0} souborů' // plural for 5 and more items
}
}
}
This module can be installed in your project using NPM or Yarn. Make sure, that you use Node.js version 6 or newer.
$ npm i plural-rules --save
$ yarn add plural-rules
Functions are exposed as named exports, for example:
import { getPluralFormForCardinal } from 'plural-rules'
You can read more about the module loading in other environments, like with ESM or in web browsers. Usage scenarios demonstrate applications of this library in typical real-world situations. Design concepts explain the approach to the correct internationalization of messages with cardinals taken by this library. Translators will read about plural rules for supported languages to be able to write the right plural forms to language packs. Data genrator enables customizing the the amount of recognized languages and thus shrink the library size. Finally, the API reference lists all functions with a description of their functionality.
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
Copyright (c) 2018-2022 Ferdinand Prantl
Licensed under the MIT license.
FAQs
Evaluates plural rules, so that localization libraries can choose the right plural form.
The npm package plural-rules receives a total of 18 weekly downloads. As such, plural-rules popularity was classified as not popular.
We found that plural-rules demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.