Is there any way to get the browser language or something like that to know which language is client using?
I'm talking about node.js =)
thanks
As with any other HTTP server, the key here is the Accept-Language
header that the browser sends with each request. See the HTTP 1.1 spec and the Accept-Language used for locale setting W3C FAQ page.
The actual code for reading the value of an HTTP varies depending on exactly which frameworks you're using ("vanilla" Node, Express, etc.).
If you happen to use the famous express server there exists middleware that does the job. request-language parses the Accept-Language
header and returns the most preferred language for you:
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'
});