[多浏览器兼容]Javascript获取浏览器语言的缺陷

来源:互联网 发布:弯曲正应力实验数据 编辑:程序博客网 时间:2024/04/29 17:32

There is a limitation for JavaScript to detect browser language(here).

1) For FF

FF support JS “navigator.language” to get the browser languages. So we can use JS to get FF language.

2) For Chrome 

There are 3 language selections for Chrome(more details here) – “Display language”, “Spell-checker language”, and “Webpage language”. The web page’s language is determined by Chrome “Webpage language”. But there is no interface to detect “Webpage language” for JS. Normally we use JS “navigator.language” to detect “Display language”. So user should set Chrome “Display language” the same as “Webpage language” to get proper language display.

3) For IE 

IE version lower than 11.0 don’t support “navigator.language”(here). So there is no way to get IE(<11.0) language. But IE support “navigator.userLanguage” to get OS language. So normally JS get IE language by “userLanguage” since most user’s OS language is the same as OS language.


Some 3rd party lib such as i18next. The lib detect language from navigator by the code below at function detectLanguage() for FF/Chrome/IE.

  • detectedLng = (navigator.language) ? navigator.language : navigator.userLanguage;

i18next-1.7.4.js

So detectLanguage() cannot get the browser language ofIE & Chrome. This is the root cause of IE & Chrome language confusion. Anyway, most of the front-end JS program get browser language by navigator since most of the user OS language is the same as browser language.

0 0
原创粉丝点击