Simplistic I18N tool for universal/isomorphic Javascript.
Usage with RequireJS
To use with RequireJS I'd advise to also use the plugin
requirejs-i18njs to be able to precompile the templates
that are in your translation files for your production code.
Usage with Handlebars
You can register your helper simply by using the .get()
function of i18njs
Handlebars.registerHelper('i18n',
function () {
return i18njs.get.apply(i18njs,
Array.prototype.slice.call(arguments, 0, -1));
}
);
then in your templates :
{{i18n 'key' data options lang}}
Installation
Either
npm install --save i18njs
or
bower install --save i18njs
Usage
Import it the way you want into your project :
var i18njs = require('i18njs');
define(['i18njs'], function (i18njs) {
});
// Global
<script type="text/javascript" src="./dist/i18njs.min.js"></script>
<script type="text/javascript">
</script>
Add locales
Your localized strings are simple json objects.
Namespaces can be as deep as you need.
var en_locales = {
'hello_world': {
'hello': 'Hello',
'world': 'World'
}
};
var fr_locales = {
'hello_world': {
'hello': 'Bonjour',
'world': 'Monde'
}
};
i18n.add('en', 'first_level_namespace', en_locales);
i18n.add('fr', 'first_level_namespace', fr_locales);
Change language
By default, language is set to en
.
i18n.setLang('fr');
Get current language
i18n.getCurrentLang();
Get dictionary
i18n.getDico();
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_level_namespace.hello_world.hello', 'en');
i18n.has('first_level_namespace.hello_world.hello');
i18n.has('en');
i18n.has('de');
i18n.has('hello_world.bye', 'en');
i18n.has('test');
List available languages
i18n.listLangs();
Get basic localized string
i18n.get('first_level_namespace.hello_world.hello');
i18n.get('first_level_namespace.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 = 5; i < max; i += 1) {}} to{{}}} {{-escape}}'
};
var data = {
'interpolate': 'Hello',
'escape': '\'<the>\' `&` "World"'
};
i18n.add('en', en_locales);
var st = i18n.get('st', data);
Change delimiters
You can also change delimiters 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 these delimiters <%=interpolate%>
, <%evaluate%>
or <%-escape%>
Add default values for templates
If you need to have a special key always replaced by the same value (a brand for example),
you can set it as a default.
This key
will be then replaced across your application's localized strings and you
won't need to pass it as a context object to your .get()
.
var fr = {
welcome: 'Bienvenue sur {{=brand}}'
};
var en = {
welcome: 'Welcome to {{=brand}}'
};
var defaults = {
fr: {
brand: 'Ma Marque'
},
en: {
brand: 'My Brand'
}
};
i18n.add('fr', fr);
i18n.add('en', en);
i18n.setDefaults(defaults);
i18n.get('welcome');
i18n.get('brand');
You don't have to use localized defaults if you don't need to :
var defaults = {
brand: 'My Brand'
};
i18n.setDefaults(defaults);
i18n.setLang('fr');
i18n.get('welcome');
You can also check your defaults :
i18n.getDefaults();