Best way to determine user's locale within browser

前端 未结 10 1346
旧巷少年郎
旧巷少年郎 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:32

    This article suggests the following properties of the browser's navigator object:

    • navigator.language (Netscape - Browser Localization)
    • navigator.browserLanguage (IE-Specific - Browser Localized Language)
    • navigator.systemLanguage (IE-Specific - Windows OS - Localized Language)
    • navigator.userLanguage

    Roll these into a javascript function and you should be able to guess the right language, in most circumstances. Be sure to degrade gracefully, so have a div containing your language choice links, so that if there is no javascript or the method doesn't work, the user can still decide. If it does work, just hide the div.

    The only problem with doing this on the client side is that either you serve up all the languages to the client, or you have to wait until the script has run and detected the language before requesting the right version. Perhaps serving up the most popular language version as a default would irritate the fewest people.

    Edit: I'd second Ivan's cookie suggestion, but make sure the user can always change the language later; not everyone prefers the language their browser defaults to.

提交回复
热议问题