Express middleware for parsing the Accept-Language header.
Install using:
npm i accept-language-middleware
or:
yarn add accept-language-middleware
Usage:
var express = require('express');
var app = express();
var acceptLanguageMiddleware = require("accept-language-middleware");
app.use(acceptLanguageMiddleware());
app.get('/', function(req, res, next) {
console.log(req.language); // 'en'
console.log(req.locale); // 'en-US'
});
Specify a default language to fallback on if none is passed:
app.use(acceptLanguageMiddleware({ default: "es" }));
Specify a set of supported languages, if the incoming Accept-Language header does not contain a language in the supported list, then the default language will be used.
app.use(acceptLanguageMiddleware({ supported: ["en", "es", "zh"] }));
Based on the awesome: https://github.com/opentable/accept-language-parser
MIT