Express middleware to handle sites in multiple languages.
The following localization setup are supported:
- authenticated user with language attribute
Ex: req.user.language
- querystring
Ex: ?hl=en
- cookie
- User-Agent
- default language
$ npm install express-tongue
var app = express();
var i18n = require('express-tongue');
app.use(i18n.localize({
endpointEnabled: true,
path: __dirname + '/i18n',
queryStringEnabled: true
}));
Returns the localization middleware using the given options
. This middleware injects
the variable i18n inside res.locals, for usage inside template engines. Also is you are
planning to use localization on client side; for example in SPA, then you have
several endpoints which helps you to develop a fully localized page.
localize()
accepts these properties in the options object.
Default language abbreviation. Defaults to en
.
Directory where localization files are located. Defaults to WORKING_DIR/i18n
.
Replace available languages for those specified in array. This is useful when you want to deactivate some language.
Enables API endpoint for usage in client apps. Defaults to false
Route in which endpoint will be mounted. Defaults to /i18n
Allows language replacement by setting key in querystring.
Setup key used in querystring to define current language. Defaults to hl
.
Cookie name for storing current locale.
When using this module with express or connect, simply app.use
the module as
high as you like. Requests that pass through the middleware will inject i18n
property
to res.locals
var i18n = require('compression')
var express = require('express')
var app = express();
var i18n = require('express-tongue');
app.use(i18n.localize({
endpointEnabled: true,
path: __dirname + '/i18n',
queryStringEnabled: true
}));
app.use('/locals', function(req, res) {
res.json(res.locals.i18n);
});