Client-side HTTP_ACCEPT_LANGUAGE access? JavaScript? I don't see a JQuery wrapper. Static pages to detect preferred language!

旧城冷巷雨未停 提交于 2019-12-08 09:56:28

问题


I'm trying to access user's language preference w/o using server code.

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

I know I can use HTTP_ACCEPT_LANGUAGE on server side, but what about in JavaScript/JQuery client-side static (non-server generated) page?

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) [A question for ServerFault.com] Use server-side URL rewriting so Apache will respond with correct static page based on language preference?

2) Use AJAX on client to query server solely to read HTTP_ACCEPT_LANGUAGE? Seems very convoluted!

Am I missing something?


回答1:


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

and

window.navigator.language for most of the others (FF, Opera, etc).

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).

Using ajax or modifying the routing as you've suggested will be only reliable way to go.



来源:https://stackoverflow.com/questions/3079158/client-side-http-accept-language-access-javascript-i-dont-see-a-jquery-wrappe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!