Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ui-lang-detector

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ui-lang-detector - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

lib/get-from-cookie.js

4

CHANGELOG.md

@@ -6,1 +6,5 @@ # Change Log

Initial release.
## [2.0.0] - 2016-10-15
* Add processing cookies.
'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();
};
};

9

package.json
{
"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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc