Internationalization with easy syntax for node.js and browser.
Classic solutions use multiple phrases for plurals. Babelfish defines plurals
inline instead - that's more compact, and easy for programmers. Also, phrases
are grouped into nested scopes, like in Ruby.
А у меня в кармане #{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
Also you can use variables in plural parts:
I have ((#{count} nail|#{count} nails))
Need special zero form or overwrite any specific value? No problems:
I have ((=0 no nails|#{count} nail|#{count} nails))
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 flatten scopes, it's really fun and nice to store translations in
YAML files:
---ru-RU: profile: Профиль forums: Форумы apps: forums: new_topic: Новая тема last_post: title : Последнее сообщение by : от demo: apples: "На столе лежит #{count} ((яблоко|яблока|яблок))"
Usage
// Create new instance of BabelFish with default language/locale: 'en-GB'var BabelFish = require('babelfish');var i18n = new BabelFish('en-GB');// Fill in some phrasesi18n.addPhrase('en-GB', 'demo.hello', 'Hello, #{user.name}.');i18n.addPhrase('en-GB', 'demo.conv.wazup', 'Whats up?');i18n.addPhrase('en-GB', 'demo.conv.alright', 'Alright, man!');i18n.addPhrase('en-GB', 'demo.coerce', 'Total: #{count}.');i18n.addPhrase('ru-RU'