https://github.com/yoannmoinet/requirejs-i18njs
:gb: A requirejs plugin to pre-compile I18NJS' translations
https://github.com/yoannmoinet/requirejs-i18njs
Last synced: 8 months ago
JSON representation
:gb: A requirejs plugin to pre-compile I18NJS' translations
- Host: GitHub
- URL: https://github.com/yoannmoinet/requirejs-i18njs
- Owner: yoannmoinet
- License: mit
- Created: 2015-08-05T19:52:06.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-10-13T03:35:37.000Z (over 10 years ago)
- Last Synced: 2025-10-11T09:31:05.906Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 191 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# requirejs-i18njs
> A [RequireJS](http://requirejs.org/) plugin to pre-compile [I18NJS](https://github.com/yoannmoinet/i18njs)' translations
[](http://badge.fury.io/js/requirejs-i18njs)
[](http://bower.io/search/?q=requirejs-i18njs)
[](https://travis-ci.org/yoannmoinet/requirejs-i18njs)
[](https://npmjs.org/package/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
```node
npm install requirejs-i18njs --save
```
or
```node
bower install requirejs-i18njs --save
```
----
## Test
```node
npm test
```
----
## Usage
### Configuration
To use with [RequireJS](http://requirejs.org/) and [I18NJS](https://github.com/yoannmoinet/i18njs).
You'll have to configure a new [package](http://requirejs.org/docs/api.html#config-packages) in your RequireJS' config.
```javascript
({
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 :
```javascript
({
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 :
```javascript
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 :
```javascript
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 :
```javascript
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](https://github.com/yoannmoinet/i18njs) imported first.
You can use your [`require.config.deps`](http://requirejs.org/docs/api.html#config-deps) for this :
```javascript
require.config({
// Note that this is at the root of the config.
deps: ['i18njs']
});
```
This will load `i18njs` before everything else.