Best way to determine user's locale within browser

前端 未结 10 1211
旧巷少年郎
旧巷少年郎 2020-11-22 09:54

I have a website (Flash) localized into a dozen of languages and I want to auto-define a default value depending on the user\'s browser settings in order to minimize the ste

10条回答
  •  无人及你
    2020-11-22 10:14

    The proper way is to look at the HTTP Accept-Language header sent to the server. This contains the ordered, weighted list of languages the user has configured their browser to prefer.

    Unfortunately this header is not available for reading inside JavaScript; all you get is navigator.language, which tells you what localised version of the web browser was installed. This is not necessarily the same thing as the user's preferred language(s). On IE you instead get systemLanguage (OS installed language), browserLanguage (same as language) and userLanguage (user configured OS region), which are all similarly unhelpful.

    If I had to choose between those properties, I'd sniff for userLanguage first, falling back to language and only after that (if those didn't match any available language) looking at browserLanguage and finally systemLanguage.

    If you can put a server-side script somewhere else on the net that simply reads the Accept-Language header and spits it back out as a JavaScript file with the header value in the string, eg.:

    var acceptLanguage= 'en-gb,en;q=0.7,de;q=0.3';
    

    then you could include a

提交回复
热议问题