Polyglot.js

Give your JavaScript the ability to speak many languages.

README

Polyglot.js


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.

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 building a global marketplace.



Polyglot is agnostic to your translation backend. It doesn’t perform any translation; it simply gives you a way to manage translated phrases from your client- or server-side JavaScript application.

Installation


install with npm:

    $ npm install node-polyglot

Running the tests


Clone the repo, run npm install, and npm test.

Usage


Instantiation


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

  1. ```js
  2. var polyglot = new Polyglot();
  3. ```

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.

See Options Overview for information about the options object you can choose to pass to new Polyglot.

Translation


Tell Polyglot what to say by simply giving it a phrases object,
where the key is the canonical name of the phrase and the value is
the already-translated string.

  1. ```js
  2. polyglot.extend({
  3.   "hello": "Hello"
  4. });

  5. polyglot.t("hello");
  6. => "Hello"
  7. ```

You can also pass a mapping at instantiation, using the key phrases:

  1. ```js
  2. var polyglot = new Polyglot({phrases: {"hello": "Hello"}});
  3. ```

Polyglot doesn’t do the translation for you. It’s up to you to give it
the proper phrases for the user’s locale.

A common pattern is to gather a hash of phrases in your backend, and output
them in a `