express-locale

Express middleware to determine locale

npm install express-locale
4 downloads in the last week
133 downloads in the last month

Build Status

express-locale

Express middleware to determine locale based on configuration and request.

Configuration (likely from a JSON file) enables mappings and lookup priorities.

Installation

npm install --save express-locale

Usage

var express = require('express'),
    locale = require('express-locale');

var app = express();
app.use(locale({
        // configuration, defaults to:
        // {
        //   "default": "en_UK",
        //   "priority": ["user-agent", "default"],
        //   "cookie": {"name": "locale"}
        // }
    }))
    .use(function (req, res) {
        res.end('Request locale: ' + req.locale.code);
    })
    .listen(3000);

Note: only full locales (language_REGION) are returned, but a mapping of languages to a default locale can be provided (see below).

Configuration

priority

Type: Array Default value ['accept-language', 'default']

Defines the order of lookups. The first lookup to return a locale will be the final result.

Available values (lookups):

  • cookie
  • domain
  • accept-language
  • default

Custom lookups can be added.

default

Type: String Default value 'en_UK'

The default locale to use (if 'default' is present in priority).

allowed

Type: Array Default value undefined

Lookup results are validated against this list of allowed locales if provided.

Type: String Default value 'locale'

If 'cookie' is present in priority, a cookie with this name is read.

Use with cookieParser middleware.

Mappings

map.language

Type: Array Default value undefined

Lookup results that return only a language can be mapped to a default locale.

map.domain

Type: Array Default value undefined

If provided and 'domain' is present in priority, the host part of the request is mapped to a locale.

Custom lookups

TODO: describe how to derive from path or querystring

npm loves you