Javascript 有没有办法使用javascript检测操作系统语言?

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

Is there anyway to detect OS language using javascript?

javascriptjavascript-framework

提问by Saleh

I need to detect OS language using javascript so I can view my page depending on the language.

我需要使用 javascript 检测操作系统语言,以便我可以根据语言查看我的页面。

I know that we can detect the browser language but that is not enough for me.

我知道我们可以检测浏览器语言,但这对我来说还不够。

I need Operation System language

我需要操作系统语言

Thanks in advance

提前致谢

回答by Richard Cook

There is no cross-browser way to do this. Internet Explorer supports the following:

没有跨浏览器的方法可以做到这一点。Internet Explorer 支持以下内容:

  • navigator.browserLanguage: browser language
  • navigator.systemLanguage: Windows system language
  • navigator.userLanguage: Windows user-specific language
  • navigator.browserLanguage: 浏览器语言
  • navigator.systemLanguage: Windows 系统语言
  • navigator.userLanguage: Windows 用户特定语言

But there is no way to access these settings from any other browsers (that I can tell) so don't use them: stick to the standard navigator.language(corresponding to the browser language) if you want to maintain cross-browser functionality. If you do use them you will tie your web site to a specific family of operating systems (i.e. Windows) and a specific browser (i.e. Internet Explorer). Do you really want to do this?

但是没有办法从任何其他浏览器(我可以告诉)访问这些设置,所以不要使用它们navigator.language如果您想维护跨浏览器功能,请坚持标准(对应于浏览器语言)。如果您确实使用它们,您会将您的网站绑定到特定的操作系统系列(即 Windows)和特定的浏览器(即 Internet Explorer)。你真的想这样做吗?

Why is the browser language insufficient for your application?

为什么浏览器语言不足以满足您的应用程序?

回答by Denis Kalinin

You mayjust guessOS languageconsidering few factors:

考虑到几个因素,您可能只是猜测操作系统语言

Windows OS

视窗操作系统

Internet Explorer

IE浏览器

  • navigator.browserLanguage: IE language (menu, help and etc.), the same as OS display language (if user hasn't change it). As in Control Panel -> Region and Language -> Keyboards and Lanugages -> Display language
  • navigator.systemLanguage: as in Control Panel -> Region and Language -> Location
  • navigator.userLanguage: as in Control Panel -> Region and Language -> Formats
  • navigator.browserLanguage: IE 语言(菜单、帮助等),与操作系统显示语言相同(如果用户未更改)。如在控制面板 -> 区域和语言 -> 键盘和语言 -> 显示语言
  • navigator.systemLanguage: 如在控制面板 -> 区域和语言 -> 位置
  • navigator.userLanguage: 如在控制面板 -> 区域和语言 -> 格式

ECMAScript Internationalization API

ECMAScript 国际化 API

var d=new Date(Date.UTC(2014,1,26,3,0,0));
var dateFormat={weekday:"long",year:"numeric",month:"long",day:"numeric"};
var result = d.toLocaleDateString("i-default",dateFormat);
alert(result);
//e.g. for Russian format ( Control Panel -> Region and Language -> Formats ) 
//result == 'среда, 26 февраля 2014 г.'

Then search resulton your server over preliminary generated set of formatted dates in different languages.

然后result在您的服务器上搜索不同语言的初步生成的格式化日期集。

NB!Chrome returns date formatted in its UI language.

注意!Chrome 返回以其 UI 语言格式化的日期。

Adobe Flash

Adobe Flash

If you desperately need to know OS language — embed flashin your page and exploit flash.system.Capabilities.language:

如果您迫切需要了解操作系统语言 -在您的页面中嵌入flash并利用flash.system.Capabilities.language

NB!Chrome doesn't allow the trick— Chrome's Flash always shows browser.language, I think because it has own Flash.

注意!Chrome 不允许这个技巧——Chrome 的 Flash 总是显示browser.language,我想是因为它有自己的 Flash。

Firefox and Chrome

火狐和 Chrome

navigator.languagetells you a browser's UI language (menu, help and etc.) and you mayassume that in overwhelming majority of cases it matches OS language (especially for home computers): while downloading FF or Chrome a download page is displayed according user's then browser — on Windows it is IE in the same language as OS.

navigator.language告诉您浏览器的 UI 语言(菜单、帮助等),您可能会假设在绝大多数情况下它与操作系统语言相匹配(尤其是对于家用计算机):在下载 FF 或 Chrome 时,会根据用户当时的浏览器显示下载页面 —在 Windows 上,它是与操作系统相同语言的 IE。

It is very strange indeed that Chrome is thing in itself when dealing with browser's environment parameters, alas.

确实很奇怪,Chrome 在处理浏览器的环境参数时本身就是一个东西,唉。

回答by Matthew Flaschen

You can use the user agent. However, it can be spoofed easily, it is not guaranteed to contain language information, and navigator.languageand navigator.browserLanguagewill probably be more reliable.

您可以使用用户代理。但是,它可以很容易地欺骗,它不保证包含语言的信息,navigator.languagenavigator.browserLanguage可能会更可靠。

回答by Denny

One thing for everyone here to consider, not all users know how to set their browser language preferences. So if you're making a site you WANT people to see, it's your problem if they land on it in a different language than one they're comfortable with, as it increases the change they will leave. A lot of it depends on the demographic you're targeting, but for a web page I think it's good to detect the system language IF you can at least, then then fall back on the browser language if you can't. It's more likely to be accurate.

这里的每个人都需要考虑一件事,并非所有用户都知道如何设置他们的浏览器语言首选项。因此,如果您正在制作一个希望人们看到的网站,那么如果他们以一种不同于他们习惯的语言访问该网站,那就是您的问题,因为这会增加他们将要离开的更改。这在很大程度上取决于您所针对的人口统计数据,但对于网页,我认为至少可以检测系统语言是很好的,如果不能,则退回到浏览器语言。它更有可能是准确的。