Javascript 获取 Chrome 的当前语言环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25606730/
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-22 21:35:55  来源:igfitidea点击:

Get current locale of Chrome

javascriptgoogle-chromelocale

提问by Amar

I am determining the current locale of the browser using this API:

我正在使用此 API 确定浏览器的当前语言环境:

var language = window.navigator.userLanguage || window.navigator.language;

This return "fr-FR"in IE, but it returns just "fr"in Chrome (and similarly for other locales).

"fr-FR"在 IE 中返回,但它只"fr"在 Chrome 中返回(对于其他语言环境也类似)。

Is there another API that will return "fr-FR"in Chrome as well?

是否还有另一个 API 也会"fr-FR"在 Chrome 中返回?

We rely on this to load the appropriate culture files.

我们依靠它来加载适当的文化文件。

回答by bm_i

Firefox and Chrome have a languagesarray on the navigatorobject. Normally the first element in that array is the users chosen locale (normally inherited from the OS settings)

Firefox 和 Chromelanguagesnavigator对象上有一个数组。通常该数组中的第一个元素是用户选择的语言环境(通常从操作系统设置继承)

var language;
if (window.navigator.languages) {
    language = window.navigator.languages[0];
} else {
    language = window.navigator.userLanguage || window.navigator.language;
}

The navigator.languagesarray in chrome for me is ["en-GB", "en-US", "en"]whereas navigator.language = "en-US", slightly different but important

navigator.languageschrome 中的数组对我来说是["en-GB", "en-US", "en"]while navigator.language = "en-US",略有不同但很重要

回答by Raveendra007

In the latest versions of Chrome browser,
it var language = window.navigator.userLanguage || window.navigator.language;returns 'fr-FR'.

在最新版本的 Chrome 浏览器中,
var language = window.navigator.userLanguage || window.navigator.language;返回 '​​fr-FR'。

If you are still facing the same issue, you can try var language = navigator.language;

如果您仍然面临同样的问题,您可以尝试 var language = navigator.language;