Client-side HTTP_ACCEPT_LANGUAGE access? JavaScrip

2019-06-13 02:09发布

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条回答
不美不萌又怎样
2楼-- · 2019-06-13 02:41

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.

查看更多
登录 后发表回答