Browser locale negotiation for node.js
locale is a node.js module for negotiating HTTP locales for incoming browser requests. It can be used as a standalone module for HTTP or as Express/Connect middleware, or as the server component for an in-browser gettext implementation like JED.
It works like this: you (optionally) tell it the languages you support, and it figures out the best one to use for each incoming request from a browser. So if you support
zh_TW, and a request comes in that accepts
en, locale will figure out that
en is the best language to use.
var http = require"http"locale = require"locale"supported = "en" "en_US" "ja"httpcreateServervar locales = reqheaders"accept-language"reswriteHeader200 "Content-Type": "text/plain"resend"You asked for: " + reqheaders"accept-language" + "\n" +"We support: " + supported + "\n" +"Our default is: " + localeLocale"default" + "\n" +"The best match is: " + localesbestsupported + "\n"listen8000
var http = require"http"express = require"express"locale = require"locale"supported = "en" "en_US" "ja"app = expressappuselocalesupportedappget"/"resheader"Content-Type" "text/plain"ressend"You asked for: " + reqheaders"accept-language" + "\n" +"We support: " + supported + "\n" +"Our default is: " + localeLocale"default" + "\n" +"The best match is: " + reqlocale + "\n"applisten8000
$ npm install locale
This module exports a function that can be used as Express/Connect middleware. It takes one argument, a list of supported locales, and adds a
locale property to incoming HTTP requests, reflecting the most appropriate locale determined using the
best method described below.
The Locale constructor takes a language tag string consisting of an ISO-639 language abbreviation and optional two-letter ISO-3166 country code, and returns an object with a
language property containing the former and a
country property containing the latter.
The default locale for the environment, as parsed from
process.env.LANG. This is used as the fallback when the best language is calculated from the intersection of requested and supported locales.
The Locales constructor takes a string compliant with the
Accept-Language HTTP header, and returns a list of acceptible locales, optionally sorted in descending order by quality score.
This method takes the target locale and compares it against the optionally provided list of supported locales, and returns the most appropriate locale based on the quality scores of the target locale. If no exact match exists (i.e. language+country) then it will fallback to
language if supported, or if the language isn't supported it will return the default locale.
supported = new locale.Locales(['en', 'en_US']); (new locale.Locales('en')).best(supported).toString(); // 'en' (new locale.Locales('en_GB')).best(supported).toString(); // 'en' (new locale.Locales('en_US')).best(supported).toString(); // 'en_US' (new locale.Locales('jp')).best(supported); // locale.Locale["default"]
Copyright (c) 2012 Jed Schmidt. See LICENSE.txt for details.
Send any questions or comments here.