cldr-plurals

1.0.0 • Public • Published

CLDR.js

CLDR.js provides logic from the Unicode Common Locale Data Repository. Currently, the only functionality it provides is the pluralization rules.

Plural Rules

Usage:

CLDR.pluralForm(0, 'en');     // => 'other'
CLDR.pluralForm(1, 'en-US');  // => 'one'
CLDR.pluralForm(2.383, 'fr'); // => 'other'
CLDR.pluralForm(1, 'zh');     // => 'other'
CLDR.pluralForm(26, 'uk');    // => 'many'

If you set CLDR.defaultLanguage then the second argument is optional:

CLDR.defaultLanguage = 'pl';
CLDR.pluralForm(22); // => 'few'

Though CLDR.js doens't provide an I18n framework, you can use this logic as part of yours. For example, you might write the following:

CLDR.defaultLanguage = 'en';

var translations = {
  'en': {
    'widget': {
      'one': 'one widget',
      'other': '{{count}} widgets'
    }
  },
  'jp': {
    'widget': {
      'other': '{{count}} ウィジェット'
    }
  }
};

function pluralize(key, count, language) {
  var form = CLDR.pluralForm(count),
      language = language || CLDR.defaultLanguage;
  return translations[language][key][form];
}

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    13
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    13

Package Sidebar

Install

npm i cldr-plurals

Weekly Downloads

13

Version

1.0.0

License

APLv2

Last publish

Collaborators

  • jamesarosen