A translation library for Derby JS.

Derby Lang

A translation library for Derby JS that uses messageformat.js.
Refer to the messageformat.js docs on how to format translation strings.

$ npm install derby-lang --save

In your server file, add the middleware:

var lang = require('derby-lang').server;

  // ...
  // ...

Add a dictionary:

// ..
// ..
.use(function (req, res, next) {
  var model = req.getModel();
  model.set('$lang.dict.strings', {
    en: {'Hello world.': 'Hello world.'},
    es: {'Hello world.': 'Hola mundo.'},
    fr: {'copyright': '© {year}'},
    it: {'foo': {'bar': 'baz'}}

Optionally, use derby-lang-fs to define a dictionary with *.json files.

In your view functions:

var lang = require('derby-lang').app;
app.proto.t = lang.translate();

In your view:

<p>{{t($lang.dict, 'en', 'Hello world.')}}</p>
<p>{{t($lang.dict, 'fr', 'copyright.', {year: 2014})}}</p>
<p>{{t($lang.dict, 'it', ['foo', 'bar'])}}

It's up to you to manage what locale to display. You can use derby-locale to automatically select the best locale for a user given the ones you support, their browser preference, etc.

A dictionary is just an object with translation strings. It can be stored in the database rather than set during a middleware route. You could subscribe to a dictionary and use ShareJS views to return only relevant languages.