requirejs-i18njs

0.2.1 • Public • Published

requirejs-i18njs

A RequireJS plugin to pre-compile I18NJS' translations

npm version bower version travis

NPM


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': /<%([\s\S]+?)%>/g,
                'interpolate': /<%=([\s\S]+?)%>/g,
                'escape': /<%-([\s\S]+?)%>/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 = require('i18n!./locales/fr.json');
i18njs.add('fr', fr);

Also, you can tell which language you want to subscribe your strings to by providing the lang as a parameter :

require('i18n!./locales/fr.json?lang=fr');

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 :

require('i18n!./locales/defaults.json?defaults=true');

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.config({
    // Note that this is at the root of the config.
    deps: ['i18njs']
});

This will load i18njs before everything else.

Readme

Keywords

none

Package Sidebar

Install

npm i requirejs-i18njs

Weekly Downloads

9

Version

0.2.1

License

MIT

Last publish

Collaborators

  • yoannmoinet