i18n with easy syntax for node.js

npm install babelfish
2 downloads in the last day
38 downloads in the last week
137 downloads in the last month

BabelFish - i18n for node.js

Build Status

Internationalisation with easy syntax for node.js. Classic solutions use multiple phrases for plurals. But we define plurals inline - that's more compact, and easier to maintain. Also, phrases are grouped into nested scopes, like in Ruby.

We support all pluralisation rules from unicode CLDR, version 2.0.1.

Phrases Syntax

  • #{varname} Echoes value of variable
  • ((Singular|Plural1|Plural2)):count Plural form


А у меня в кармане #{nails_count} ((гвоздь|гвоздя|гвоздей)):nails_count

You can also omit anchor variable for plurals, by default it will be count. Thus following variants are equal:

  • I have #{count} ((nail|nails))
  • I have #{count} ((nail|nails)):count

Escape chars

If you need #{, ((, | or )) somewhere in text, where it can be considered as markup part - just escape them with \.

Example with YAML

As BabelFish supports scopes, it's really fun and nice to store translations in YAML files:

  profile: Профиль
  forums: Форумы
      new_topic: Новая тема
        title : Последнее сообщение
        by : от
    apples: "На столе лежит #{apples.count} ((яблоко|яблока|яблок)):apples.count"


// Create new instance of BabelFish with default language/locale: 'en-GB'
var i18n = require('babelfish').create('en-GB');

// Fill in some phrases
i18n.addPhrase('en-GB', 'demo.hello',         'Hello, #{}.');
i18n.addPhrase('en-GB', 'demo.conv.wazup',    'Whats up?');
i18n.addPhrase('en-GB', 'demo.conv.alright',  'Alright, man!');

i18n.addPhrase('ru-RU', 'demo.hello',         'Привет, #{}.');
i18n.addPhrase('ru-RU', 'demo.conv.wazup',    'Как дела?');

i18n.addPhrase('uk-UA', 'demo.hello',         'Здоровенькі були, #{}.');

// Set locale fallback so we use most appropriate translation
i18n.setFallback('uk-UA', 'ru-RU');

// Translate
var params = {user: {name: 'ixti'}};

i18n.t('ru-RU', 'demo.hello', params);  // -> 'Привет, ixti.'
i18n.t('ru-RU', 'demo.conv.wazup');     // -> 'Как дела?'
i18n.t('ru-RU', 'demo.conv.alright');   // -> 'Alright, man!'

i18n.t('uk-UA', 'demo.hello', params);  // -> 'Здоровенькі були, ixti.'
i18n.t('uk-UA', 'demo.conv.wazup');     // -> 'Как дела?'
i18n.t('uk-UA', 'demo.conv.alright');   // -> 'Alright, man!'

// You may want to get "compiled" translations to export them into browser.
// -> {
//      'demo.hello'        : { type: 'function', translation: [Function] },
//      'demo.conv.wazup'   : { type: 'string', translation: 'Как дела?' },
//      'demo.conv.alright' : { type: 'string', translation: 'Alright, man!' }
//    }

// Or use `i18n.stringify(locale)` to get string, eval-able to object.

NOTICE BabelFish#getCompiledData just exports an object with strings/functions. You are responsible to serialize it and then inject into browser runtime. You can use BabelFish#stringify if you are ok with defaults.

Assuming that you have serialized data and it's available on browser as i18nData, you can do following to inject them into i18n (on browser):

<script type="text/javascript" src="/assets/babelfish-runtime.js"></script>
<script type="text/javascript" src="/assets/"></script>
<script type="text/javascript">
  var i18n = new BabelFish('en-GB');

  // We assume `` exports `i18nData` global variable
  i18n._storage['ru-RU'] = i18nData;


View the LICENSE file (MIT).

npm loves you