requirejs-i18njs
Overview
This plugins lets you pre-compile the templates in your translation files.
You won't have any unsafe eval in your production code and will gain some execution time too.
Install
Either
npm install requirejs-i18njs --save
or
bower install requirejs-i18njs --save
Test
npm test
Usage
Configuration
To use with RequireJS and I18NJS.
You'll have to configure a new package in your RequireJS' config.
packages: 'name': 'i18n' // The location where the package is installed 'location': './node_modules/requirejs-i18njs/src/' // The main file 'main': 'requirejs-i18njs'
You can also configure some new delimiters for your templates :
packages: 'name': 'i18n' 'location': './node_modules/requirejs-i18njs/src/' 'main': 'requirejs-i18njs' 'delimiters': 'evaluate': /<%%>/g 'interpolate': /<%=%>/g 'escape': /<%-%>/g
This will result in pre-compiling delimiters in the form of <%=interpolate%>
, <%evaluate%>
or <%-escape%>
.
Usage
You'll then be able to import your locales with :
var fr = ;i18njs;
Also, you can tell which language you want to subscribe your strings to by providing the lang
as a parameter :
;
It will execute i18n.add('fr', locales);
directly from the plugin.
The same is also possible to add new defaults to your configuration by providing the defaults
parameter :
;
It will execute i18n.setDefaults(defaults);
directly from the plugin.
For these to work, you'll need to have I18NJS imported first.
You can use your require.config.deps
for this :
require;
This will load i18njs
before everything else.