ui-lang-detector
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -6,1 +6,5 @@ # Change Log | ||
Initial release. | ||
## [2.0.0] - 2016-10-15 | ||
* Add processing cookies. |
28
index.js
'use strict'; | ||
const util = require('./lib/util'); | ||
const normalizeOptions = require('./lib/normalize-options'); | ||
const getFromCookie = require('./lib/get-from-cookie'); | ||
const getFromHeader = require('./lib/get-from-header'); | ||
module.exports = function (options) { | ||
util.normalizeOptions(options); | ||
const normOptions = normalizeOptions(options); | ||
return function (req, res, next) { | ||
req.uilang = util.getPreferableLang(req.headers['accept-language']) || | ||
util.getDefaultLang(); | ||
req.uilang = ''; | ||
if (normOptions.cookieName) { | ||
const result = getFromCookie(normOptions.cookieName, req); | ||
if (result) { | ||
req.uilang = result; | ||
next(); | ||
return; | ||
} | ||
} | ||
const result = getFromHeader(req); | ||
if (result) { | ||
req.uilang = result; | ||
next(); | ||
return; | ||
} | ||
req.uilang = normOptions.defaultLang; | ||
next(); | ||
}; | ||
}; |
{ | ||
"name": "ui-lang-detector", | ||
"version": "1.0.0", | ||
"description": "Express.js middleware to detect the UI language that a User Agent prefers analyzing the Accept-Language HTTP header", | ||
"version": "2.0.0", | ||
"description": "Express.js middleware to detect UI language to be used to serve content", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "semistandard && node_modules/.bin/tape tests/*.js | tap-spec" | ||
"test": "semistandard && node_modules/.bin/tape 'test/*.js' | tap-spec", | ||
"test:dev": "node_modules/.bin/tape 'test/*.js' | tap-spec" | ||
}, | ||
@@ -16,2 +17,3 @@ "author": { | ||
"devDependencies": { | ||
"cookie-parser": "^1.4.3", | ||
"express": "^4.13.4", | ||
@@ -26,2 +28,3 @@ "morgan": "^1.7.0", | ||
"keywords": [ | ||
"UI language", | ||
"Accept-Language", | ||
@@ -28,0 +31,0 @@ "Express middleware" |
@@ -5,8 +5,16 @@ # ui-lang-detector | ||
Express.js middleware to detect the UI language that a User Agent prefers analyzing the Accept-Language HTTP header. | ||
Express.js middleware to detect UI language to be used to serve content. | ||
As soon as you support a website that serves multilingual UI, you often may want to send a first-time visitor the content in the language most appropriate to them. Later on, you will have a chance to ask the user what language they prefer and store this information with the user account data, or send a special cookie to the user agent. But the first time you meet your user, all what you have is the `Accept-Language` header in their HTTP request. | ||
As soon as you support a website that serves multilingual UI, you want to send the user your content in the language the user prefer. If this is a first-time visitor, you may want to try to guess the most appropriate language from the `Accept-Language` header. Later on, you will have a chance to ask the user what language they prefer and store this information with the user account data, or send a special cookie to the user agent. | ||
This middleware analyzes the `Accept-Language` header and stores the most preferable language's code in `req.uilang`. __All language codes are in lowercase.__ | ||
This middleware extends the Request object with `req.uilang` property following these steps: | ||
* First, check if the special cookie has come with the request. If so, ok - use it. | ||
* If no cookie detected, analyze the `Accept-Language` header and choose the most preferable language. | ||
* If nothing helps, use the provided default value. | ||
__All language codes are lowercased__ (e.g., `en`, `en-us` etc). | ||
## Installation | ||
@@ -26,3 +34,4 @@ | ||
const options = { | ||
defaultLang: "en" | ||
cookieName: 'lang', | ||
defaultLang: 'en' | ||
}; | ||
@@ -46,2 +55,8 @@ | ||
#### options.cookieName | ||
Type: `String` | ||
Optional. The cookie name being used to store UI language. If omitted, cookies are not processed. | ||
#### options.defaultLang | ||
@@ -51,2 +66,2 @@ | ||
Default language code to use if no information is available in a request. | ||
Default language code to use. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6356
10
94
64
8