tinganho / express-request-language Goto Github PK
View Code? Open in Web Editor NEWA middleware to figure out a request's language tag by parsing Accept-Language header and stored cookies
License: MIT License
A middleware to figure out a request's language tag by parsing Accept-Language header and stored cookies
License: MIT License
If you perform a GET request to /languages/
(notice ending slash and that 'language' param is empty), cookie is set as an empty string.
$ curl -s -D - http://localhost:3000/languages/ | grep -i 'Set-Cookie'
Set-Cookie: language=; Path=/
This even applies when you set a language that is not configured in languages
option:
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', 'es'],
cookie: {
name: 'language',
url: '/languages/{language}'
}
}));
...and perform such a request:
$ curl -s -D - http://localhost:3000/languages/en | grep -i 'Set-Cookie'
Set-Cookie: language=en; Path=/
$ curl -s -D - http://localhost:3000/languages/notSupportedLanguage | grep -i 'Set-Cookie'
Set-Cookie: language=notSupportedLanguage; Path=/
In my opinion, middleware should validate that the received language is actually supported (in the languages
array) before setting the cookie.
Hello,
In this lines: https://github.com/tinganho/express-request-language/blob/master/index.js#L94-L100
You are outputing user input, really bad ideia. This can be exploited.
Show only the message: "The language is not supported." I think it's enough.
If I try to curl http://localhost:3000
my server, I get this error:
TypeError: Cannot read property 'split' of undefined
at parseAndSortLanguageTags (.../node_modules/accept-language/Build/Source/AcceptLanguage.js:81:40)
at AcceptLanguage.parse (.../node_modules/accept-language/Build/Source/AcceptLanguage.js:42:43)
at AcceptLanguage.get (.../node_modules/accept-language/Build/Source/AcceptLanguage.js:36:21)
at set (.../node_modules/express-request-language/index.js:23:46)
at .../node_modules/express-request-language/index.js:116:5
at Layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (.../node_modules/express/lib/router/index.js:312:13)
at .../node_modules/express/lib/router/index.js:280:7
at Function.process_params (.../node_modules/express/lib/router/index.js:330:12)
at next (.../node_modules/express/lib/router/index.js:271:10)
This error is new and I suspect is introduced by the recent work on the sub-package node-accept-language
.
I don't know the best solution, but I personally think that the middleware should fallback to the default locale if no accept-language header is found. Let me know what you think!
I think the @types/express dependency should be moved to devDependencies since it is not used by the consumers of this library.
I can't import import * as requestLanguage from 'express-request-language';
when using Typescript 2.5.2. I get this error
[ts] Module '"/node_modules/express-request-language/index"' resolves to a non-module entity and cannot be imported using this construct.
We need a way to unset the language for testing.
Would like to set language through Set language URL but some languages are not able to set. For example, en-IN
.
It won't change the language in the cookie and won't redirect.
It would be nice if have some way to pass a param like queryName
to set a default language.
So, if there isn't a cookie already set and if you pass a prop like: defaultLanguage
, then set the default language. I think it could be a very nice feature.
Example:
const languages = ['en', 'pt']
server.use(
requestLanguage({
languages: languages,
queryName: 'lang',
defaultLanguage: 'en',
cookie: {
name: 'DEFAULT_LANGUAGE',
options: { maxAge: 24 * 3600 * 1000 }
}
})
)
The first time if there is no cookie already set, it would be 'en', ignoring the accept language...
WDYT about it?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.