我一直在尝试使用JavaScript检测浏览器语言首选项。
如果我在IE的“工具>Internet选项>常规>语言”中设置浏览器语言,如何使用JavaScript读取此值?
火狐也有同样的问题。我无法检测工具>选项>内容>语言使用导航器.language的设置。
使用导航器。userLanguage,它检测设置通过
打开>ControlPanel>RegionalandLanguageOptions>Regional Options选项卡。
我已经用navigator测试过了。browserLanguage和navigator。systemLanguage但都不返回第一个设置的值(Tools>InternetOptions>General>Languages)
我找到了一个详细讨论这个问题的链接,但这个问题仍然没有答案:(
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没有列出它
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没有列出它
我有一个黑客,我认为使用很少的代码,是相当可靠的。
把网站的文件放到子目录中。SSL到您的服务器中,并创建到存储文件的子目录的符号链接,该子目录指示您的语言。
就像这样:
ln -s /var/www/yourhtml /var/www/en
ln -s /var/www/yourhtml /var/www/sp
ln -s /var/www/yourhtml /var/www/it
使用您的web服务器读取HTTP_ACCEPT_LANGUAGE并根据它提供的语言值重定向到这些“不同的子目录”。
现在您可以使用Javascript的window.location.href来获取url,并在条件中使用它来可靠地识别首选语言。
url_string = window.location.href;
if (url_string = "http://yoursite.com/it/index.html") {
document.getElementById("page-wrapper").className = "italian";
}