{"id":22423370,"url":"https://github.com/prantlf/plural-rules","last_synced_at":"2025-08-01T07:32:19.860Z","repository":{"id":34292286,"uuid":"153942739","full_name":"prantlf/plural-rules","owner":"prantlf","description":"Evaluates plural rules, so that localization libraries can choose the right plural form.","archived":false,"fork":false,"pushed_at":"2022-12-21T16:36:09.000Z","size":442,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-11T20:56:20.689Z","etag":null,"topics":["localization","localize","plural","plural-form","plural-forms","plural-rules","pluralize","plurals"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prantlf.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-20T19:51:42.000Z","updated_at":"2023-12-20T04:42:59.000Z","dependencies_parsed_at":"2023-01-15T06:00:47.536Z","dependency_job_id":null,"html_url":"https://github.com/prantlf/plural-rules","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fplural-rules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fplural-rules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fplural-rules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prantlf%2Fplural-rules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prantlf","download_url":"https://codeload.github.com/prantlf/plural-rules/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228348371,"owners_count":17905899,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["localization","localize","plural","plural-form","plural-forms","plural-rules","pluralize","plurals"],"created_at":"2024-12-05T18:10:45.513Z","updated_at":"2024-12-05T18:10:48.073Z","avatar_url":"https://github.com/prantlf.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Plural Rules\n\n[![Latest version](https://img.shields.io/npm/v/plural-rules)\n ![Dependency status](https://img.shields.io/librariesio/release/npm/plural-rules)\n](https://www.npmjs.com/package/plural-rules)\n[![Coverage](https://codecov.io/gh/prantlf/plural-rules/branch/master/graph/badge.svg)](https://codecov.io/gh/prantlf/plural-rules)\n\nEvaluates 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.\n\n* 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.)\n* 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.)\n* Generated from the official [CLDR plural rules] version 36.0. Cardinals for [almost 200 languages](./docs/languages.md#supported-languages) are supported.\n* Minimal interface for finding out the right [plural form](./docs/design.md#plural-forms) by evaluating [plural rules](./docs/design.md#plural-rules) for a specific [locale](./docs/design.md#locales). Looking up and formatting localizable strings is a task for internationalization libraries.\n\nIf you are looking for a smaller and [faster](https://github.com/prantlf/fast-plural-rules/blob/master/docs/speed.md#plural-form-lookup-speed) library using [Mozilla plural rules], see [fast-plural-rules].\n\n### Table of Contents\n\n- [Synopsis](#synopsis)\n- [Installation and Getting Started](#installation-and-getting-started)\n- [Usage Scenarios](./docs/usage.md#usage-scenarios)\n- [Design Concepts](./docs/design.md#design-concepts)\n- [Supported Languages](./docs/languages.md#supported-languages)\n- [API Reference](./docs/API.md#api-reference)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Synopsis\n\n```js\nimport { getPluralFormForCardinal } from 'plural-rules'\n\n// Returns index of the plural form for the specified locale and cardinal.\ngetPluralFormForCardinal('en', 1) // Returns \"one\";   \"1 file\"\ngetPluralFormForCardinal('en', 2) // Returns \"other\"; \"2 files\"\ngetPluralFormForCardinal('en', 5) // Returns \"other\"; \"5 files\"\ngetPluralFormForCardinal('cs', 1) // Returns \"one\";   \"1 soubor\"\ngetPluralFormForCardinal('cs', 2) // Returns \"few\";   \"2 soubory\"\ngetPluralFormForCardinal('cs', 5) // Returns \"other\"; \"5 souborů\"\n\n// Returns a localized message for the specified locale and cardinal.\nlocalizeMessage('en', 'fileCount', 3) // Returns \"3 files\"\nlocalizeMessage('cs', 'fileCount', 3) // Returns \"3 soubory\"\n\n// Returns a localized message for the specified locale and cardinal.\nfunction localizeMessage (locale, messageKey, cardinal) {\n  const pluralForm = getPluralFormForCardinal(locale, cardinal)\n  const messageFormat = messages[locale][messageKey][pluralForm]\n  return messageFormat.replace('{0}', cardinal)\n}\n// Localized messages organized by locales and message keys.\nconst messages = {\n  en: {\n    fileCount: {\n      one:   '{0} file', // singular\n      other: '{0} files' // plural\n    }\n  },\n  cs: {\n    fileCount: {\n      one:   '{0} soubor',  // singular\n      few:   '{0} soubory', // plural for 2-4 items\n      other: '{0} souborů'  // plural for 5 and more items\n    }\n  }\n}\n```\n\n## Installation and Getting Started\n\nThis module can be installed in your project using [NPM] or [Yarn]. Make sure, that you use [Node.js] version 6 or newer.\n\n```sh\n$ npm i plural-rules --save\n```\n\n```sh\n$ yarn add plural-rules\n```\n\nFunctions are exposed as named exports, for example:\n\n```js\nimport { getPluralFormForCardinal } from 'plural-rules'\n```\n\nYou can read more about the [module loading](./docs/API.md#loading) in other environments, like with ESM or in web browsers. [Usage scenarios](./docs/usage.md#usage-scenarios) demonstrate applications of this library in typical real-world situations. [Design concepts](./docs/design.md#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](./docs/languages.md#supported-languages) to be able to write the right plural forms to language packs. [Data genrator](#./API.md#data-generator) enables customizing the the amount of recognized languages and thus shrink the library size. Finally, the [API reference](./docs/API.md#api-reference) lists all functions with a description of their functionality.\n\n## Contributing\n\nIn 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.\n\n## License\n\nCopyright (c) 2018-2022 Ferdinand Prantl\n\nLicensed under the MIT license.\n\n[Node.js]: http://nodejs.org/\n[NPM]: https://www.npmjs.com/\n[Yarn]: https://yarnpkg.com/\n[CLDR plural rules]: http://cldr.unicode.org/index/cldr-spec/plural-rules\n[Mozilla plural rules]: https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals#List_of_Plural_Rules\n[fast-plural-rules]: https://github.com/prantlf/fast-plural-rules\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprantlf%2Fplural-rules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprantlf%2Fplural-rules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprantlf%2Fplural-rules/lists"}