"Summary" when doing an internationalized Web project, I encountered a problem of switching the default language version according to the natural language used by the user's browser. So, organized this article.
First , W3cschool about navigator The various attribute values are very clear, here no longer repeat.
then , The differences are analyzed from the Navigator objects of each browser on the attributes of the language.
1.Google Chrome's Navigator:
As you can see, Google Chrome has two attributes for languages: language and languages. The former is to return to the current browser language, which is the browser's all available language versions (Google Chrome can add the required language in the advanced settings and can switch to the language you want to use.) In this way, it is possible to test the ability to set the current language version automatically by default in multi-language versions.
2. Firefox's navigator:
Firefox also has two properties for languages: language and languages.
The navigator of 3.IE8 and IE9:
(1) IE8 and the following versions:
IE8 has two properties:systemlanguage and userlanguage. The former is the default language that returns the current operating system, which is the natural language that returns the operating system settings.
And look at IE9 and above:
IE9 has three properties:systemlanguage: Returns the default language of the current operating system;Userlanguage: Returns the natural language set by the operating system;Browserlanguage : Returns the current browser language.
"Summary" the various browsers support the language related attributes in the following table:
Property |
IE8 and the following versions |
IE9 and above versions |
Firefox/chrome/safari |
Opera |
Language |
X |
X |
√ |
√ |
Userlanguage |
√ |
√ |
X |
√ |
Systemlanguage |
√ |
√ |
X |
X |
Browserlanguage |
X |
√ |
X |
√ |
You can use the following code to get the current browser language:
(Navigator.language | | navigator.browserlanguage). toLowerCase ()
Navigator objects about the properties of a language