i18n for node
A ground up rebuild of the i18next project for a node environment.
The i18next project has a number of issues that result from it being a port of an originally client-side project. In particular that it maintains a single global datastore, and so if two modules resolve to the same instance then they will interfere with each other.
The aim of this project is to create a module-safe, lightweight translation library for a node environment, based on similar concepts to i18next.
npm install [--save] i18n-future
First create some resource files. These should be json files and the location of the file within your project will define the language it corresponds to.
An example file structure would look like:
index.js
/locales
/en
/default.json
/fr
/default.json
/de
/default.json
If you wish to create additional namespaces within your project, then create additional files within a language directory with names corresponding to the namespace. For details on how to configure resource paths see "Resource path" documentation below
var i18n = require('i18n-future');
// i18n fires a "ready" event when it is done loading resources
i18n.on('ready', function () {
i18n.translate('name.first', 'fr');
});
var app = require('express')();
var i18n = require('i18n-future').middleware();
app.use(i18n);
app.use(function (req, res, next) {
// a translate method is now available on the request
// this will translate keys according to the language request headers
res.render('index', {
title: req.translate('title')
});
});
app.listen(3000);
Middleware will detect the language from the Accepts
header by default, but if custom language detection is required then additional middleware can be added which sets req.lang
.
var app = require('express')();
var i18n = require('i18n-future').middleware();
app.use(function (req, res, next) {
// load language from querystring parameter - e.g. ?lang=en
req.lang = req.query.lang;
next();
});
app.use(i18n);
...
The translate method takes an additional options object with parameters for language and namespace to be loaded. This applies to both standalone and express middleware options (although in the middleware case any language option will overwrite the accepts header as a language preference)
If the options are set to an array or a string, then this will be used as the language.
// simple language option
i18n.translate('greeting', 'fr');
// multiple langauge options as an array
i18n.translate('greeting', ['en-GB', 'en']);
If multiple langauges are passed, then the first matching language will be used. For example: i18n.translate('greeting', ['en-GB', 'en']);
will look for en-GB
first, and fall back to en
only if a translation for en-GB
is not found.
// simple namespace option
i18n.translate('greeting', { namespace: 'admin' });
For running code examples, look in the examples directory.
In each case options can be passed both to the i18n function, or to the middleware constructor equivalently.
You can manually define resource sets at initialisation by passing in an object with the resources
parameter of the options. This is useful when you have shared translations that are used across a number of modules, so you may wish to load from npm modules or similar - Default: {}
i18n({
resources: {
en: require('shared-translations').en,
fr: require('shared-translations').fr
}
});
This object should have the same structure as the resources returned by backends
Set the language which is used as the fallback when none is specified, or the requested key does not exist in the requested language - Default: 'en'
i18n({
fallbackLng: 'en-GB'
});
Set the namespace which is used as the fallback when none is specified, or the requested key does not exist in the requested namespace - Default: 'default'
i18n({
fallbackNamespace: 'admin'
});
If required, both the fallbackLng
and fallbackNamespace
options can be passed as an array.
For the fs resource loader (currently the only backend supported), sets the location to load resource files from, and pattern for parsing namespace and language from the file path - Default: locales/__lng__/__ns__.json
.
i18n({
path: '/var/i18n/__lng__/__ns__/resource.json'
});
The root directory in which to look for resources on the path
defined - Default: process.cwd()
i18n({
baseDir: __dirname
});
Note: if you are using i18n-future inside a module which is likely to be installed as a child dependency of another project then it is highly recommended that you set this property.
Allows setting a custom backend for non-fs resource loading. Backend must export a load
method, which will be called with options
object and a callback. Default: fs resource loader
i18n({
backend: {
load: function (options, callback) {
// do custom resource loading
callback(null, resources);
}
}
});
Resources returned by the callback will be an object of the following form:
{
en: {
default: {
... translation keys for default namespace ...
}
'other-namespace': {
... translation keys for afternative namespace...
}
},
fr: {
...
},
de: {
...
}
}