Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

plural-rules

prantlf75MIT2.0.1TypeScript support: included

Evaluates plural rules, so that localization libraries can choose the right plural form.

plural-rules, plural-forms, plurals, plural, pluralize, localize

readme

Plural Rules

Latest version Dependency status Coverage

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.

  • Tiny code base - 2.52 kB, 1.57 KB minified, 0.67 KB gzipped. Do not pack unnecessary weight in your application. (Bundled code for the web browser is 11 kB, 4.77 kB minified, 1.79 kB gzipped.)
  • Packed data - 18.2 kB, 16 kB minified, 3 kB gzipped. A quarter of the size of the original CLDR data. (This size adds to the code size.)
  • Generated from the official CLDR plural rules version 36.0. Cardinals for almost 200 languages are supported.
  • Minimal interface for finding out the right plural form by evaluating plural rules for a specific locale. Looking up and formatting localizable strings is a task for internationalization libraries.

If you are looking for a smaller and faster library using Mozilla plural rules, see fast-plural-rules.

Table of Contents

Synopsis

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
    }
  }
}

Installation and Getting Started

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.

Contributing

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.

License

Copyright (c) 2018-2022 Ferdinand Prantl

Licensed under the MIT license.

changelog

2.0.1 (2022-12-21)

Bug Fixes

  • Complete the command-line help (83ffb85)

2.0.0 (2022-08-18)

Features

  • API for checking supported locales and plural forms (0b8fadc)
  • Declare CJS, UMD and ES exports in package.json (8beef35)

BREAKING CHANGES

  • Newer Node.js required, different command-line parsing, renamed CJS exports.
  • Node.js 14.8 or newer is required for the create-plural-data script. Also supports type, module, types and exports. The sources of the library didn't change, which means that the exported modules should still work in Node.js 6 and newer, but the're no tests proving that.
  • Command-line argument parsing done by a custom code instead of using commander. Boolean arguments cannot be joined to groups like -mpv. They have to be passed separately, for example: -m -p -v. Also, the command-line script requires Node.js with the ESM support, which means >= 14.8.
  • CJS modules in the dist directory end with the file extension .cjs instead of .js. (ES modules end with .mjs.) Usually imported main exports are not affected, because they are imported in both CJS and ES modules using the package name.

1.0.2 (2022-01-28)

Bug Fixes

  • Adapt sources after upgrading the build environment (9f4021e)

1.0.1 (2019-09-24)

Bug Fixes

  • Upgrade package dependencies (bf8bfa6)

1.0.0 (2019-08-03)

Features

  • Improve the data compression by serialising plural form objects to strings (858c2a0)

BREAKING CHANGES

  • Plural data created by this version of the library cannot be consumed by older versions. It would be unusual, if you created data by tis version and fed them to an older version of the library, but nevertheless, be aware of it. If you create data by this version, use them with the same major version or a newer one, as long as it is documented to be feasible.

Plural data created by older versios of this library can still be used with this version. The code is beckwards compatible.

2019-08-03 v1.0.0

Decrease the size of the packed plural rules and forms

0.1.0 (2019-08-03)

Bug Fixes

  • Transpile the CJS module output for Node.js 6 or newer (6fb15aa)

Features

  • Upgrade CLDR data to the version 35.1
  • Provide both minificated and only cleaned-up UMD scripts for the browser (460efad)

0.0.2 (2019-06-07)

Bug Fixes

  • Upgrade module dependencies (a76acb4)

2018-11-05 v0.0.1

Initial release