Skip to content

Instantly share code, notes, and snippets.

@slavafomin
Last active February 12, 2023 19:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slavafomin/f2e5259cab17d55af5d9fa4c2c2baa08 to your computer and use it in GitHub Desktop.
Save slavafomin/f2e5259cab17d55af5d9fa4c2c2baa08 to your computer and use it in GitHub Desktop.
How to pluralize words in JavaScript in different languages?

How to pluralize any word in different languages using JavaScript?

Hello!

Today, I'm going to show you how to pluralize any word in JavaScript (Node.js or Browser) in almost any language!

I've developed a very simple, but powerful package in JavaScript that will help you to achieve this goal. It's called Numerous.

I'm going to start with Node.js example.

Pluralizing words in Node.js

Installation

First of all, let's start by installing the required dependency:

npm i -S numerous

A Simple Example

Then, let's create a simple Node script:

const numerous = require('numerous');

// Returns "apple".
numerous.pluralize('en', 1, {
  one: 'apple',
  other: 'apples',
});

// Returns "cats".
numerous.pluralize('en', 2, {
  one: 'cat',
  other: 'cats',
});

API Explained

numerous.pluralize(localeId, value, variants) static function accepts three required arguments:

  • localeId — a locale ID, e.g. en for English, ru for Russian, etc.
  • value — a value your pluralization should be based on, e.g.: 1, 10, 100500
  • variants — is an object that holds all possible pluralization forms of the word you want to pluralize

The complete list of all supported languages and locales could be found in Numerous locales document.

Key names for all possible variants could be found in the specific locale file. For example for English locale it could be found in English locale definition file.

e.g. for English the variant keys are:

  • one — single form of the word, e.g. "child"
  • other — plural form of the word, e.g. "children"

Loading Locales

In Node.js, the Numerous will load all the locales automatically when requested and cache them for later use. Locale definitions files are loaded only on demand (lazy-loaded) to preserve memory.

However, in Browser you will need to load each required locale manually before using it.

Using Instantiation

It's easy to use the static form of the API explained above, however, it's also possible to create an instance of the pluralizer in order to preconfigure it with some specific locale. It will allow you to reduce number of arguments you will require to pass when calling the pluralization function.

So, to create an instance of specific locale pluralizer call: numerous.create(localeId);, e.g. numerous.create('en');.

Then you can use it's pluralize(value, variants) method with only two arguments:

// Creating instances for specific locales
const englishPluralizer = numerous.create('en');
const russianPluralizer = numerous.create('ru');

// Returns "apple"
englishPluralizer.pluralize(1, {
  one: 'apple',
  other: 'apples',
});

// Returns "яблока"
russianPluralizer.pluralize(2, {
  one: 'яблоко',
  few: 'яблока',
  many: 'яблок',
});

Pluralizing words in Browser using vanilla JavaScript

Please see the demo of how you could use this word pluralization library in browser environment with just vanilla JavaScript.


I hope this small article and the library itself will help you to make word pluralization easy in any JavaScript environment.

If you have any questions — please post them in the comments. I will be glad to help! Cheers!

@UniBreakfast
Copy link

numerous.pluralize(locale, calue, variants)
calue instead of value

@slavafomin
Copy link
Author

numerous.pluralize(locale, calue, variants)
calue instead of value

Fixed, thanks!

And I've also updated the library, check it out ;)

@peterjarian
Copy link

numerous.pluralize(locale, calue, variants)
calue instead of value

Fixed, thanks!

And I've also updated the library, check it out ;)

bro that was 2.5 years ago lmao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment