Best way to determine user's locale within browser

前端 未结 10 1215
旧巷少年郎
旧巷少年郎 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 <script src> pointing at that external service in the HTML, and use JavaScript to parse the language header. I don't know of any existing library code to do this, though, since Accept-Language parsing is almost always done on the server side.

    Whatever you end up doing, you certainly need a user override because it will always guess wrong for some people. Often it's easiest to put the language setting in the URL (eg. http​://www.example.com/en/site vs http​://www.example.com/de/site), and let the user click links between the two. Sometimes you do want a single URL for both language versions, in which case you have to store the setting in cookies, but this may confuse user agents with no support for cookies and search engines.

    0 讨论(0)
  • 2020-11-22 10:14

    On Chrome and Firefox 32+, navigator.languages contains an array of locales in order of user preference, and is more accurate than navigator.language, however to make it backwards-compatible (Tested Chrome / IE / Firefox / Safari), then use this:

    function getLang()
    {
     if (navigator.languages != undefined) 
     return navigator.languages[0]; 
     else 
     return navigator.language;
    }
    
    0 讨论(0)
  • 2020-11-22 10:19

    I did a bit of research regarding this & I have summarised my findings so far in below table

    So the recommended solution is to write a a server side script to parse the Accept-Language header & pass it to client for setting the language of the website. It's weird that why the server would be needed to detect the language preference of client but that's how it is as of now There are other various hacks available to detect the language but reading the Accept-Language header is the recommended solution as per my understanding.

    0 讨论(0)
  • 2020-11-22 10:24

    You can also try to get the language from the document should might be your first port of call, then falling back to other means as often people will want their JS language to match the document language.

    HTML5:

    document.querySelector('html').getAttribute('lang')

    Legacy:

    document.querySelector('meta[http-equiv=content-language]').getAttribute('content')

    No real source is necessarily 100% reliable as people can simply put in the wrong language.

    There are language detection libraries that might let you determine the language by content.

    0 讨论(0)
  • 2020-11-22 10:27

    You said your website has Flash, then, as another option, you can get operation system's language with flash.system.Capabilities.language— see How to determine OS language within browser to guess an operation system locale.

    0 讨论(0)
  • 2020-11-22 10:28

    I used all the answers and created a single line solution:

    const getLanguage = () => navigator.userLanguage || (navigator.languages && navigator.languages.length && navigator.languages[0]) || navigator.language || navigator.browserLanguage || navigator.systemLanguage || 'en';
    
    console.log(getLanguage());
    
    0 讨论(0)
提交回复
热议问题