问题描述
我正在尝试使用服务器代码来访问用户的语言首选项。
I'm trying to access user's language preference w/o using server code.
我正在寻找像这样的JavaScript:
I'm looking for some JavaScript like this:
var language_array = jQuery.languagePreferences();
//en-ca,en;q=0.8,en-us;q=0.6,de-de;q=0.4,de;q=0.2
我知道我可以在服务器端使用HTTP_ACCEPT_LANGUAGE,但是在JavaScript / JQuery客户端静态(非服务器生成的)页面中呢?
I know I can use HTTP_ACCEPT_LANGUAGE on server side, but what about in JavaScript/JQuery client-side static (non-server generated) page?
我已经做了相当多的谷歌搜索并没有明确的浏览器无关解决方案.. JavaScript如何获得浏览器的首选用户语言列表? (例如,使用工具>选项>内容>语言在FireFox中配置)这似乎太难了。
I've done a fair amount of Googling and have no clear browser agnostic solution.. how does JavaScript gain access to browsers' list of preferred user languages? (as configured, say, in FireFox with Tools > Options > Content > Languages) It seems this is too hard.
如果不可能,这些似乎可行:
If it's not possible it seems these might work:
1)[ServerFault.com的问题]使用服务器端URL重写,以便Apache根据语言偏好响应正确的静态页面?
1) [A question for ServerFault.com] Use server-side URL rewriting so Apache will respond with correct static page based on language preference?
2)在客户端上使用AJAX来查询服务器,只读取HTTP_ACCEPT_LANGUAGE?看起来很复杂!
2) Use AJAX on client to query server solely to read HTTP_ACCEPT_LANGUAGE? Seems very convoluted!
我错过了什么吗?
推荐答案
不幸的是它不是可以使用纯客户端技术获得相当于 HTTP_ACCEPT_LANGUAGE
。您可以获得的最接近的是用户当前的操作系统区域设置:
Unfortunately it isn't possible to get at the equivalent of HTTP_ACCEPT_LANGUAGE
using purely client side techniques. The closest you can get to it is the users current OS locale settings:
navigator.userLanguage
for IE
和
window.navigator.language
其他人(FF,Opera等)。
window.navigator.language
for most of the others (FF, Opera, etc).
我发现 navigator.language
和 navigator.browserLanguage
不太可靠(例如IE8错误地给了我 en-us 而不是 en-gb )。
I find that navigator.language
and navigator.browserLanguage
to be not very reliable (e.g. IE8 is incorrectly giving me en-us rather than en-gb).
使用ajax或按照你的建议修改路由只是可靠的方式。
Using ajax or modifying the routing as you've suggested will be only reliable way to go.
这篇关于客户端HTTP_ACCEPT_LANGUAGE访问? JavaScript的?我没有看到JQuery包装器。静态页面检测首选语言!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!