Best way to determine user's locale within browser

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

提交回复
热议问题