i18njs
A simple i18n for Javascript with a templating feature.
Installation
Either
npm install --save i18njs
or
bower install --save i18njs
Usage
After importing it var i18n = require('i18njs');
Add locales
var en_locales = {
'hello_world': {
'hello': 'Hello',
'world': 'World'
}
};
var fr_locales = {
'hello_world': {
'hello': 'Bonjour',
'world': 'Monde'
}
};
i18n.add('en', 'first_test', en_locales);
i18n.add('fr', 'first_test', fr_locales);
Change language
By default, language is set to en
.
i18n.lng = 'fr';
Check for availability
If needed, you can also check for the presence of a specific localized string in a particular language.
You can check only the language too.
i18n.has('first_test.hello_world.hello', 'en');
i18n.has('en');
Get basic localized string
i18n.get('first_test.hello_world.hello');
i18n.get('first_test.hello_world.hello', 'fr');
Get templated string
It uses a basic templating engine, the same as underscore.
It works in the form of {{=interpolate}}
, {{evaluate}}
or {{-escape}}
:
var en_locales = {
'st': '{{=interpolate}} {{for(var i = 0, max = 1; i < max; i += 1) {}}to{{}}} {{-escape}}'
};
var data = {
'interpolate': 'Hello',
'escape': '\'<the>\' `&` "World"'
};
i18n.add('en', en_locales);
var st = i18n.get('st', data);
You can also change delimeters by passing the third options
arguments
var st = i18n.get('st', data, {
evaluate: /<%([\s\S]+?)%>/g;
interpolate: /<%=([\s\S]+?)%>/g;
escape: /<%-([\s\S]+?)%>/g;
});
Will result in <%=interpolate%>
, <%evaluate%>
or <%-escape%>