Javascript 如何在 node.js (express.js) 中获取浏览器语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11845471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the browser language in node.js (express.js)?
提问by Oleg Dats
User requests some page and I want to know (on server side) what is the language in his/her browser. So I could render template with the right messages.
用户请求一些页面,我想知道(在服务器端)他/她的浏览器中的语言是什么。所以我可以用正确的消息渲染模板。
On client side it's easy:
在客户端很容易:
var language = window.navigator.userLanguage || window.navigator.language
回答by Joachim Isaksson
You can use req.headers["accept-language"]to get the language/locale the user has set in his browser.
您可以使用req.headers["accept-language"]获取用户在浏览器中设置的语言/区域设置。
For easier support, you may want to look into a locale module.
为了更容易获得支持,您可能需要查看locale 模块。
回答by cGuille
request.acceptsLanguages
will contain a parsed version of request.headers['accept-language']
.
request.acceptsLanguages
将包含request.headers['accept-language']
.
回答by jgrocha
With Express 4.x, you can use the build in req.acceptsLanguages(lang [, ...])to check if certain languages are accepted.
使用 Express 4.x,您可以使用req.acceptsLanguages(lang [, ...]) 中的构建来检查是否接受某些语言。
var express = require('express');
app.get('/translation', function(request, response) {
var lang = request.acceptsLanguages('fr', 'es', 'en');
if (lang) {
console.log('The first accepted of [fr, es, en] is: ' + lang);
...
} else {
console.log('None of [fr, es, en] is accepted');
...
}
});
To get the list of all accepted languages, using Express 4.x, you can use the module accepts.
要获取所有接受语言的列表,使用 Express 4.x,您可以使用模块accepts。
var express = require('express'), accepts = require('accepts');
app.get('/translation', function(request, response) {
console.log(accepts(request).languages());
...
});
回答by einstein
You would need to parse the string in req.headers["accept-language"]
. Which will give you a priority list of preferred languages from the client. You can also check req.acceptsLanguages(lang [, ...])
if your language is supported or not.
您需要解析req.headers["accept-language"]
. 这将为您提供客户首选语言的优先列表。您还可以检查req.acceptsLanguages(lang [, ...])
您的语言是否受支持。
I would strongly recommend to use express-request-languageto do any language matching work, since it could be very difficult to get it right at the first time.
我强烈建议使用express-request-language来做任何语言匹配工作,因为第一次就很难做到正确。
Most of the time, matching a language is not enough. A user might want to change a preferred language. express-request-language
help you store a preferred language in a cookie it also gives your server a URL path to change a preferred language.
大多数时候,匹配一种语言是不够的。用户可能想要更改首选语言。express-request-language
帮助您在 cookie 中存储首选语言它还为您的服务器提供了一个 URL 路径来更改首选语言。
All above functionality can be done with just a couple of lines of code:
只需几行代码即可完成上述所有功能:
app.use(requestLanguage({
languages: ['en-US', 'zh-CN'],
cookie: {
name: 'language',
options: { maxAge: 24*3600*1000 },
url: '/languages/{language}'
}
}));
In case of no match, the middleware will also match a default language (en-US
above).
如果不匹配,中间件也会匹配默认语言(en-US
如上)。