Introduction
This is a middleware to use i18next in express.js.
Getting started
Source can be loaded via npm.
# npm package
$ npm install i18next-express-middleware
wire up i18next to request object
var i18next = ;var middleware = ;var express = ; i18next; var app = ;app; // in your request handlerapp; // in your views, eg. in pug (ex. jade)div = ;
add routes
// missing keys; make sure the body is parsed (i.e. with [body-parser](https://github.com/expressjs/body-parser#bodyparserjsonoptions))app; // multiload backend routeapp;
add localized routes
You can add your routes directly to the express app
var express = app = i18next = FilesystemBackend = i18nextMiddleware = port = 3000; i18next ;app;app;
or to an express router
var express = app = i18next = FilesystemBackend = i18nextMiddleware = router = port = 3000; i18next ;app;app;
language detection
Detects user language from current request. Comes with support for:
- path
- cookie
- header
- querystring
- session
Wiring up:
var i18next = ;var middleware = ; i18next;
As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.
Detector Options
// order and from where user language should be detected order: /*'path', 'session', */ 'querystring' 'cookie' 'header' // keys or params to lookup language from lookupQuerystring: 'lng' lookupCookie: 'i18next' lookupHeader: 'accept-language' lookupSession: 'lng' lookupPath: 'lng' lookupFromPathIndex: 0 // cache user language caches: false // ['cookie'] // optional expire and domain for set cookie cookieExpirationDate: cookieDomain: 'myDomain' cookieSecure: true // if need secure cookie
Options can be passed in:
preferred - by setting options.detection in i18next.init:
var i18next = ;var middleware = ; i18next;
on construction:
var middleware = ;var lngDetector = null options;
via calling init:
var middleware = ; var lngDetector = ;lngDetector;
Adding own detection functionality
interface
moduleexports = name: 'myDetectorsName' { // options -> are passed in options return 'en'; } { // options -> are passed in options // lng -> current language, will be called after init and on changeLanguage // store it };
adding it
var i18next = ;var middleware = ; var lngDetector = ;lngDetector; i18next;