Socket
Socket
Sign inDemoInstall

express-request-language

Package Overview
Dependencies
3
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    express-request-language

Figure out a request's language tag by parsing Accept-Language header and stored cookies.


Version published
Weekly downloads
5.7K
increased by1.04%
Maintainers
1
Install size
50.6 kB
Created
Weekly downloads
 

Readme

Source

request-language Build Status

A middleware to figure out your request's language either by parsing Accept-Language header or by looking at a language cookie's value. request-language plays nicely with L10ns by abstracting all your language setting logic for you.

Installation:

npm install express-request-language --save

Usage

Use it as a middleware to express. All options are described below. Your language will be accessed with req.language.

var requestLanguage = require('express-request-language');
var cookieParser = require('cookie-parser');
var express = require('express');
var app = express();

app.use(cookieParser());
app.use(requestLanguage({
  languages: ['en-US', 'zh-CN'],
  cookie: {
    name: 'language',
    options: { maxAge: 24*3600*1000 },
    url: '/languages/{language}'
  }
}));

app.get('/', function(req, res, next) {
  console.log(req.language); // 'en-US'
});
...

Usage with L10ns

Access all your localizations in req.localizations.

var requestLanguage = require('express-request-language');
var cookieParser = require('cookie-parser');
var express = require('express');
var localizations = require('path/to/l10ns/output/all');
var app = express();

app.use(cookieParser());
app.use(requestLanguage({
  languages: ['en-US', 'zh-CN'],
  cookie: {
    name: 'language',
    options: { maxAge: 24*3600*1000 },
    url: '/languages/{language}'
  },
  localizations: localizations
}));

app.get('/', function(req, res, next) {
  // It will use localization from the right language.
  var l = req.localizations;
  console.log(l('HELLO_WORLD'));
});
...

Options

languages {Array}

Define your language tags ordered in highest priority comes first fashion. The language tags must comply with BCP47 standard. The BCP47 language tag consist of at least the following subtags:

  1. A language subtag (en, zh).
  2. A script subtag (Hant, Latn).
  3. A region subtag (US, CN).

Then language tag has the following syntax:

language[-script][-region]

Which makes the following language tags en, en-US and zh-Hant-TW all BCP47 compliant. Please note that the script tag refers to language script. Some languages use two character sets instead of one. Chinese is a good example of having two character sets instead of one–it has both traditional characters and simplified characters. And for popular languages that uses two or more scripts please specify the script subtag, because it can make an i18n library fetch more specific locale data.

Setting the cookie property is optional and whenever it is set this middleware will look at the cookie value instead of the Accept-Language header. Setting this cookie property is ideal for application that support more than 1 language and allows users to change language. This option requires that you uses express' cookie middleware cookie-parser.

cookie.name

Name of the language cookie. It will store the current language tag of the user's session and remain until maxAge expires or changed by cookie.url.

cookie.options (optional)

The options are the same options as express uses in res.cookie(name, value. options). Please checkout their documentation.

cookie.url (optional)

Set the change language URL. Lets say that you set the value to /languages/{language} in your configurations. If you visit with your browser the URL path /languages/en-US. It will change your language cookie value to en-US. It will redirect back to the origin URL if you send a referrer header and default to / if it don't send a referrer header.

queryName (optional) {Object}

You can optionally set the language using a query string. This option allows you to set the name of the query parameter that triggers the language setting. The default value is language.

The selected language can be unset by setting the language parameter to default.

var middleware = requestLangauge({
  languages: ['en-US', 'zh-CN'],
  queryName: 'locale', // ?locale=zh-CN will set the language to 'zh-CN'
  cookie: {
    name: 'language'
  }
});
localizations (optional) {Function}

Set the L10ns requireLocalizations(language) function. The right language tag will be used and automatically figured out by request-language. L10ns' l() function will be accessible through express' req.localizations. You also need to set a scoped l variable before usage, otherwise L10ns can't update localization keys through code traversal:

app.get('/', function(req, res, next) {
  var l = req.localizations;
});

Maintainer

Tingan Ho @tingan87

License

MIT

Keywords

FAQs

Last updated on 31 Oct 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc