有没有办法使用javascript检测操作系统语言?
我需要使用 javascript 检测操作系统语言,以便我可以根据语言查看我的页面。
我知道我们可以检测浏览器语言,但这对我来说还不够。
我需要操作系统语言
提前致谢
I need to detect OS language using javascript so I can view my page depending on the language.
I know that we can detect the browser language but that is not enough for me.
I need Operation System language
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这将返回系统语言:
This returns the system language:
没有跨浏览器的方法可以做到这一点。 Internet Explorer 支持以下语言:
navigator.browserLanguage
:浏览器语言navigator.systemLanguage
:Windows 系统语言navigator.userLanguage
:Windows 用户特定语言但是无法从任何其他浏览器访问这些设置(据我所知),因此不要使用它们:坚持使用标准
navigator.language
(对应于浏览器语言)如果您想维护跨浏览器功能。如果您确实使用它们,您会将您的网站与特定系列的操作系统(即 Windows)和特定浏览器(即 Internet Explorer)联系起来。你真的想这样做吗?为什么浏览器语言不足以满足您的应用程序?
There is no cross-browser way to do this. Internet Explorer supports the following:
navigator.browserLanguage
: browser languagenavigator.systemLanguage
: Windows system languagenavigator.userLanguage
: Windows user-specific languageBut 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?Why is the browser language insufficient for your application?
您可能只是猜测操作系统语言,考虑几个因素:
Windows 操作系统
Internet Explorer
navigator.browserLanguage
:IE 语言(菜单、帮助等),与操作系统显示语言相同(如果用户没有更改)。如控制面板->地区和语言 ->键盘和语言 ->显示语言navigator.systemLanguage
:如控制面板 -> 中所示地区和语言 ->位置navigator.userLanguage
:如控制面板 -> 中所示地区和语言 ->格式ECMAScript 国际化 API
然后在服务器上搜索初步生成的不同语言的格式化日期集的
结果
。注意! Chrome 返回以其 UI 语言格式化的日期。
Adobe Flash
如果您迫切需要了解操作系统语言 - 在页面中嵌入 flash 并利用 flash.system.Capability.language:
注意! Chrome 不允许这种伎俩 — Chrome 的 Flash 总是显示
browser.language
,我想是因为它有自己的 Flash。Firefox 和 Chrome
navigator.language
告诉您浏览器的 UI 语言(菜单、帮助等),您可能假设在绝大多数情况下它与操作系统语言匹配(尤其是对于家用电脑):下载 FF 或 Chrome 时,会根据用户当时的浏览器显示下载页面 - 在 Windows 上,它是与操作系统语言相同的 IE。确实很奇怪,Chrome 在处理浏览器的环境参数时本身就是一个东西,唉。
You may just guess OS language considering few factors:
Windows OS
Internet Explorer
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 languagenavigator.systemLanguage
: as in Control Panel -> Region and Language -> Locationnavigator.userLanguage
: as in Control Panel -> Region and Language -> FormatsECMAScript Internationalization API
Then search
result
on your server over preliminary generated set of formatted dates in different languages.NB! Chrome returns date formatted in its UI language.
Adobe Flash
If you desperately need to know OS language — embed flash in your page and exploit 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.Firefox and Chrome
navigator.language
tells you a browser's UI language (menu, help and etc.) and you may assume 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.It is very strange indeed that Chrome is thing in itself when dealing with browser's environment parameters, alas.
您可以使用用户代理。但是,它很容易被欺骗,并且不能保证包含语言信息,并且
navigator.language
和navigator.browserLanguage
可能会更可靠。You can use the user agent. However, it can be spoofed easily, it is not guaranteed to contain language information, and
navigator.language
andnavigator.browserLanguage
will probably be more reliable.您可以通过以下方式从js获取默认语言和支持的语言:
navigator.language
Return 'en-GB'
navigator .languages
返回['en-GB', 'en-US', 'en', 'zh-CN']
PS:我在Chrome上测试过在 Windows 上(不确定 Mac 是否也能同样工作)
You can get from the js with default languages, and supported languages through this:
navigator.language
Return 'en-GB'
navigator.languages
Returns ['en-GB', 'en-US', 'en', 'zh-CN']
PS: I tested on Chrome on Windows (Not sure if Mac also works the same)
这里每个人都需要考虑一件事,并非所有用户都知道如何设置浏览器语言首选项。因此,如果您正在制作一个希望人们看到的网站,那么如果他们以与他们熟悉的语言不同的语言登陆该网站,那就是您的问题了,因为这会增加他们留下的更改。这在很大程度上取决于您的目标人群,但对于网页,我认为如果您至少可以检测系统语言,那么最好是检测系统语言,如果不能,则重新使用浏览器语言。它更有可能是准确的。
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.