如何使用 JavaScript 获取浏览器语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8199760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:04:36  来源:igfitidea点击:

How to get the browser language using JavaScript

javascript

提问by ghs.alshareef

Possible Duplicate:
JavaScript for detecting browser language preference

可能的重复:
用于检测浏览器语言偏好的 JavaScript

I want to detect the language of the browser that is entering my site, if it's En or Fr. So I can redirect to the En page or the other page.

我想检测进入我网站的浏览器的语言,如果它是 En 或 Fr。所以我可以重定向到 En 页面或其他页面。

Also, can I detect mobile language?

另外,我可以检测移动语言吗?

回答by Alim Ul Gias

Try this script to get your browser language

试试这个脚本来获取你的浏览器语言

<script type="text/javascript">
var userLang = navigator.language || navigator.userLanguage; 
alert ("The language is: " + userLang);
</script>

Cheers

干杯

回答by zzzzBov

The "JavaScript" way:

“JavaScript”方式:

var lang = navigator.language || navigator.userLanguage; //no ?s necessary

Really you should be doing language detection on the server, but if it's absolutely necessary to know/use via JavaScript, it can be gotten.

确实您应该在服务器上进行语言检测,但是如果绝对有必要通过 JavaScript 了解/使用,则可以获取。