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

发布于 2024-09-27 09:01:02 字数 114 浏览 0 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

电影里的梦 2024-10-04 09:01:02

这将返回系统语言:

Intl.DateTimeFormat().resolvedOptions().locale

This returns the system language:

Intl.DateTimeFormat().resolvedOptions().locale
你在看孤独的风景 2024-10-04 09:01:02

没有跨浏览器的方法可以做到这一点。 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 language
  • navigator.systemLanguage: Windows system language
  • navigator.userLanguage: Windows user-specific language

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?

Why is the browser language insufficient for your application?

五里雾 2024-10-04 09:01:02

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

Windows 操作系统

Internet Explorer

  • navigator.browserLanguage:IE 语言(菜单、帮助等),与操作系统显示语言相同(如果用户没有更改)。如控制面板->地区和语言 ->键盘和语言 ->显示语言
  • navigator.systemLanguage:如控制面板 -> 中所示地区和语言 ->位置
  • navigator.userLanguage:如控制面板 -> 中所示地区和语言 ->格式

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 г.'

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

注意! 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 language
  • navigator.systemLanguage: as in Control Panel -> Region and Language -> Location
  • navigator.userLanguage: as in Control Panel -> Region and Language -> Formats

ECMAScript Internationalization 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 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.

以酷 2024-10-04 09:01:02

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

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

孤凫 2024-10-04 09:01:02

您可以通过以下方式从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)

梦中楼上月下 2024-10-04 09:01:02

这里每个人都需要考虑一件事,并非所有用户都知道如何设置浏览器语言首选项。因此,如果您正在制作一个希望人们看到的网站,那么如果他们以与他们熟悉的语言不同的语言登陆该网站,那就是您的问题了,因为这会增加他们留下的更改。这在很大程度上取决于您的目标人群,但对于网页,我认为如果您至少可以检测系统语言,那么最好是检测系统语言,如果不能,则重新使用浏览器语言。它更有可能是准确的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文