Node Roundup: Internationalisation

2011-05-25 00:00:00 +0100 by Alex R. Young


Dialect (License: MIT, npm: dialect) by Pau Ramon Revilla is an internationalisation API for Node that stores translations in memory. The translations are periodically
synced to MongoDB, so it's fast and easy to serve multiple clients with.

The author's example demonstrates this:

var dialect = require('dialect').dialect({current_locale: 'es', store: {mongodb: {}}});

// connects to the store
dialect.connect(function () {

  // syncs the memory dictionaries with the store
  dialect.sync({interval:3600}, function (err, foo) {
    d.get('Hello World!'); // => Hola mundo

The mongodb property expects an object with
database, host, and other connection options.
I'd prefer to see a connection URI here, but it's fairly easy to get
going with MongoDB or SQLite.

The API has a lot of features that I've come to expect from a good i18n
library -- plurals, contexts, and interpolation are all supported.

The best thing about Dialect, however, is
dialect-http (License: MIT, npm: dialect-http). This is a nice little web app that translators can
use to find missing translations and fill them in. It's one of the
better open source examples of this type of tool that I've seen.

I'm having some trouble running dialect-http; I think the author might
need to try running it on a fresh npm install with a blank database.


node-jus-i18n (npm: jus-i18n) by Nicolas Chambrier also supports plurals, context, and interpolation. It comes with support for various data stores: module,
file, gettext, and he's also working on various databases. The
documentation is still a little bit confusing, but he's written a lot of
examples and future plans.

There's integration with Express, which means req objects
get an i18n object:

app.configure(function() {
  i18n.enableForApp(app, { // options (all are optional, you can pass {} or undefined
    "locale": "en",          // default locale
    "catalogue": "messages", // catalogue to load
    "locales": undefined,    // locales to load
  }, function(err) { // called when i18n has loaded messages


And there are helpers for use in templates:

_('Hello, {name}', {name: userName});
plural('You have {n} messages', nbMessages);


Polyglot (npm: polyglot) by Ricardo Tomasi is aimed squarely at Express apps. Ricardo suggests some helper aliases in the README:

    __: i18n.translate
  , languages: i18n.languages
  , n: i18n.plural // optional

As you can see, it does basic translation lookups and plurals. I'm not
sure what's going on with the README, because it refers to
express- and express-voyage as package names,
but I installed it with polyglot as suggested by the
package.json file.