npm i express-mw-lang


Language-helper middleware for Express web server.

by volebo

2.0.2 (see all)License:MITTypeScript:Not FoundCategories:Express Internationalization, Express Localization
npm i express-mw-lang


Build Status codecov bitHound Overall Score bitHound Dependencies npm version npm downloads

Localization helper for Express web server.

Cuts off the language part from the path, and allow to use simple routes!

This middleware helps to determine the language, and handles urls of incoming requests for next middlewares. express-mw-lang does not use Accept-Language (but it is a good idea, to use the value of this HTTP-Header on first contact with the user, to predict his language).

To determine the language express-mw-lang uses only URL of the request. Expected format of the URL:

schema://host/< LANG >/path?params

Sample URL, handled in French:

As you could see - lang is a first part of the path, and could be ommited, in this case the default language will be used.

Actually, express-mw-lang acts and is an express-router. The best practice is appending your router/routes (use/route/get/post/...) to the express-mw-lang, instead of habitual way with the Express-application itself.

See the Example section for more details.

Contributions are welcome!



$ npm install express-mw-lang

Configure and use in the app.js:

// ...
var langGen = require('express-mw-lang');
var app = express();

// ...

let langmw = langGen();


"use strict";

var express         = require('express');
var logger          = require('morgan');          // just for example, not required
var bodyParser      = require('body-parser');     // just for example, not required
var langGen         = require('express-mw-lang');

var app = express();

app.use(bodyParser.urlencoded({ extended: false }));

// STEP 1: CREATING the middleware instance with options:
var langmw = langGen({
    defaultLanguage: 'en',
    availableLanguages: ['en', 'ru'],
// STEP 2: append middleware to application
// ( `esu` - is a reverse of `use`, because app and mw are swapped):

// appending main routes to the app (through lang-mw):
var router = express.Router();
    .get(function(req, res, next){
        res.status(200).send('Hello [' + req.lang.code + ']');
// STEP 3: append your routes to lang-mw

app.listen(3000, function() {
    console.log('Example app listening on port 3000!');

// -> Hello [en]
// -> Hello [ru]
// -> Hello [en]


const options = {
    defaultLanguage: 'en',
    availableLanguages: [],
    onLangDetected: function(langCode, req, res) {}


Default: 'en', string (ISO 639).

Default language


Default: [], array of strings (each string - ISO 639 code).

Languages, which will be recognized by middleware.

  1. Default language is always available
  2. For requests to unavailable language the response will contain redirect to the / of the site.


Defalt: null, function(langCode, req, res)

Callback, called when the lang is determined. Convenient place to setup the locale for momentjs or choosen i18n library.


var i18n            = require("i18n")
var moment          = require('moment')

// ...

let options = {
    defaultLanguage: 'en',
    availableLanguages: ['en', 'ru'],
    onLangDetected: function(lang_code, req, res) {

        i18n.setLocale(req, lang_code)
        i18n.setLocale(res, lang_code)


req and res extensions

This middleware extends the req and res objects with a lang property, so extensions are:

  1. req.lang
  2. res.locals.lang

Further - description of the lang object.


string, default language, taken from options.


array of available options. Each item is an object with code-property (string, ISO 639)


bool indicates, that request is handled using default language (probably, there was an error, when the MW tried to determine the required language of the request).


function(local_path, lang_code) , this function could help you to build paths to other pages of the site (taking the current language into account).

Notes about argument lang_code:

  1. it is optional
  2. UNKNOWN/UNAVAILABLE lang_code - cause to use the default lang
  3. NULL lang_code - cause to use CURRENT lang (determined by MW from URL)
  4. EMPTY ('') lang_code - cause to use default lang!!


You could take part in the development process, just follow this guideline.


Please, read the LICENSE file in the root of the repository (or downloaded package).