Polyglot.js is a tiny I18n helper library written in JavaScript, made to
work both in the browser and in CommonJS environments (Node). It provides a simple solution for interpolation and pluralization, based off of Airbnb's
experience adding I18n functionality to its Backbone.js and Node apps. Polyglot has zero dependencies.

I18n is incredibly important for us at Airbnb,
as we have listings in 192 countries, and we translate our site into 30-odd different languages.
We're also hiring talented engineers
to help us scale up to meet the challenges of buliding a global marketplace.

$ npm install
$ npm test
t
✓ should translate a simple string
✓ should return the key if translation not found
✓ should interpolate
✓ should interpolate the same placeholder multiple times
✓ should allow you to supply default values
◦ should return the non-interpolated key if not initialized with allowMissing ✓ should return the non-interpolated key if not initialized with allowMissing and translation not found
◦ should return an interpolated key if initialized with allowMissing and trans ✓ should return an interpolated key if initialized with allowMissing and translation not found
✓ should support nested phrase objects
pluralize
✓ should support pluralization with an integer
✓ should accept a number as a shortcut to pluralize a word
locale
✓ should default to 'en'
✓ should get and set locale
extend
✓ should support multiple extends, overriding old keys
✓ shouldn't forget old keys
✓ should support optional `prefix` argument
✓ should support nested object
clear
✓ should wipe out old phrases
replace
✓ should wipe out old phrases and replace with new phrases
✔ 18 tests complete (12ms)

First, create an instance of the Polyglot class, which you will use for translation.

var polyglot =newPolyglot();

Polyglot is class-based so you can maintain different sets of phrases at the same time, possibly in different locales. This is very useful for example when serving requests with Express, because each request may have a different locale, and you don't want concurrent requests to clobber each other's phrases.

For pluralization to work properly, you need to tell Polyglot what the current locale is. You can use polyglot.locale("fr") to set the locale to, for example, French. This method is also a getter:

polyglot.locale()

=>"fr"

You can also pass this in during instantiation.

var polyglot =newPolyglot({locale:"fr"});

Currently, the only thing that Polyglot uses this locale setting for is pluralization.

Polyglot provides a very basic pattern for providing
pluralization based on a single string that contains all plural forms for a given phrase. Because various languages have different nominal forms for zero, one, and multiple, and because the noun can be before or after the count, we have to be overly explicit
about the possible phrases.

To get a pluralized phrase, still use polyglot.t() but use a specially-formatted phrase string that separates the plural forms by the delimeter ||||, or four vertical pipe characters.

For pluralizing "car" in English, Polyglot assumes you have a phrase of the form:

polyglot.extend({

"num_cars":"%{smart_count} car |||| %{smart_count} cars",

});

English (and German, Spanish, Italian, and a few others) there are only two plural forms: singular and not-singular.

Some languages get a bit more complicated. In Czech, there are three separate forms: 1, 2 through 4, and 5 and up. Russian is even crazier.

polyglot.t() will choose the appropriate phrase based
on the provided smart_count option, whose value is a number.

Completely replace the existing phrases with a new set of phrases.
Normally, just use extend to add more phrases, but under certain
circumstances, you may want to make sure no old phrases are lying around.

Breaking change: Moved from Singleton pattern to class-based. Now you create an instance of the Polyglot class rather than using class methods directly on it. The reason is to allow maintaining multiple sets of phrases, which is something we ran into at Airbnb with a highly-concurrent Express app.

Breaking change: Removed the built-in Handlebars helpers, because Handlebars is a singleton, and it's messy to create a single helper function that can be bound to different Polyglot instances. Instead, it's super easy to create your own, based on your requirements.