我一直在尝试使用JavaScript检测浏览器语言首选项。
如果我在IE的“工具>Internet选项>常规>语言”中设置浏览器语言,如何使用JavaScript读取此值?
火狐也有同样的问题。我无法检测工具>选项>内容>语言使用导航器.language的设置。
使用导航器。userLanguage,它检测设置通过
打开>ControlPanel>RegionalandLanguageOptions>Regional Options选项卡。
我已经用navigator测试过了。browserLanguage和navigator。systemLanguage但都不返回第一个设置的值(Tools>InternetOptions>General>Languages)
我找到了一个详细讨论这个问题的链接,但这个问题仍然没有答案:(
我一直在使用Hamid的答案一段时间,但在语言数组像["en", "en- gb ", "en- us ", "fr- fr", "fr", "en- za "]的情况下,它将返回"en",当"en- gb "将是一个更好的匹配。
我的更新(下面)将返回第一个长格式的代码,例如。"en-GB",否则将返回第一个短代码,例如。"en",否则返回null。
function getFirstBrowserLanguage() {
var nav = window.navigator,
browserLanguagePropertyKeys = ['language', 'browserLanguage', 'systemLanguage', 'userLanguage'],
i,
language,
len,
shortLanguage = null;
// support for HTML 5.1 "navigator.languages"
if (Array.isArray(nav.languages)) {
for (i = 0; i < nav.languages.length; i++) {
language = nav.languages[i];
len = language.length;
if (!shortLanguage && len) {
shortLanguage = language;
}
if (language && len>2) {
return language;
}
}
}
// support for other well known properties in browsers
for (i = 0; i < browserLanguagePropertyKeys.length; i++) {
language = nav[browserLanguagePropertyKeys[i]];
//skip this loop iteration if property is null/undefined. IE11 fix.
if (language == null) { continue; }
len = language.length;
if (!shortLanguage && len) {
shortLanguage = language;
}
if (language && len > 2) {
return language;
}
}
return shortLanguage;
}
console.log(getFirstBrowserLanguage());
更新:当某些属性未定义时,IE11出现错误。增加了跳过这些属性的检查。
I can't find a single reference that state that it's possible without involving the serverside.
MSDN上:
navigator.browserLanguage
navigator.systemLanguage
navigator.userLanguage
从browserLanguage:
In Microsoft Internet Explorer 4.0 and
earlier, the browserLanguage property
reflects the language of the installed
browser's user interface. For example,
if you install a Japanese version of
Windows Internet Explorer on an
English operating system,
browserLanguage would be ja.
In Internet Explorer 5 and later,
however, the browserLanguage property
reflects the language of the operating
system regardless of the installed
language version of Internet Explorer.
However, if Microsoft Windows 2000
MultiLanguage version is installed,
the browserLanguage property indicates
the language set in the operating
system's current menus and dialogs, as
found in the Regional Options of the
Control Panel. For example, if you
install a Japanese version of Internet
Explorer 5 on an English (United
Kingdom) operating system,
browserLanguage would be en-gb. If you
install Windows 2000 MultiLanguage
version and set the language of the
menus and dialogs to French,
browserLanguage would be fr, even
though you have a Japanese version of
Internet Explorer.
Note This property does not indicate
the language or languages set by the
user in Language Preferences, located
in the Internet Options dialog box.
此外,它看起来像是browserLanguage被弃用了,因为IE8没有列出它
我一直在使用Hamid的答案一段时间,但在语言数组像["en", "en- gb ", "en- us ", "fr- fr", "fr", "en- za "]的情况下,它将返回"en",当"en- gb "将是一个更好的匹配。
我的更新(下面)将返回第一个长格式的代码,例如。"en-GB",否则将返回第一个短代码,例如。"en",否则返回null。
function getFirstBrowserLanguage() {
var nav = window.navigator,
browserLanguagePropertyKeys = ['language', 'browserLanguage', 'systemLanguage', 'userLanguage'],
i,
language,
len,
shortLanguage = null;
// support for HTML 5.1 "navigator.languages"
if (Array.isArray(nav.languages)) {
for (i = 0; i < nav.languages.length; i++) {
language = nav.languages[i];
len = language.length;
if (!shortLanguage && len) {
shortLanguage = language;
}
if (language && len>2) {
return language;
}
}
}
// support for other well known properties in browsers
for (i = 0; i < browserLanguagePropertyKeys.length; i++) {
language = nav[browserLanguagePropertyKeys[i]];
//skip this loop iteration if property is null/undefined. IE11 fix.
if (language == null) { continue; }
len = language.length;
if (!shortLanguage && len) {
shortLanguage = language;
}
if (language && len > 2) {
return language;
}
}
return shortLanguage;
}
console.log(getFirstBrowserLanguage());
更新:当某些属性未定义时,IE11出现错误。增加了跳过这些属性的检查。
对于这个问题,DanSingerman有一个很好的解决方案。
该语言的唯一可靠来源是http请求头。
因此,您需要一个服务器端脚本来回复请求报头,或者至少是Accept-Language字段。
这是一个非常简单的Node.js服务器,它应该与DanSingermans的jQuery插件兼容。
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(req.headers));
}).listen(80,'0.0.0.0');