JavaScript如何检测默认语言环境? [英] How/Where does JavaScript detect the default locale?

查看:114
本文介绍了JavaScript如何检测默认语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,2017年9月11日,JavaScript的 toLocaleDateString()方法正在输出 9/11/2017 为了我。但我在英国,所以在这种情况下格式化是错误的。 MDN Web Docs 告诉我此方法返回默认语言环境中的格式化字符串。

Today, the 11th of September, 2017, JavaScript's toLocaleDateString() method is outputting 9/11/2017 for me. But I am in the UK, so the formatting is wrong in this case. The MDN Web Docs tell me that this method returns "a formatted string in the default locale".

那么,JavaScript在哪里/如何检测我的默认语言环境?这个设置在哪里,或它依赖什么,以及(如何)我可以更改它?

So, where/how is JavaScript detecting my default locale? Where is this set, or what does it depend on, and (how) can I change it?

编辑添加:我在Windows上使用Firefox 55.0.3 10,如果这有任何区别。

Edited to add: I'm using Firefox 55.0.3 on Windows 10 if that makes any difference.

推荐答案

总而言之,检测当前的语言环境取决于实现,可能因环境而异。您的默认语言也可能取决于您用于安装浏览器的安装程序。

To summarize shortly, detecting the current locale is implementation dependent and may differ from environment to environment. Your default language may also depend on the installer you've used to install your browser.

不那么短的版本:

遵循ECMAScript规范,符合浏览器(和其他环境,如Node.js)应该 ECMAScript之后实施本地化国际化API(ECMA-402),仅为获取默认语言环境

Following the ECMAScript spec, conforming browsers (and other environments, such as Node.js) should implement localization following the ECMAScript Internationalization API (ECMA-402), which only outlines the following for getting the default locale:


DefaultLocale抽象操作返回表示的字符串值[... ] 主机环境当前语言环境的语言标记。

The DefaultLocale abstract operation returns a String value representing the [...] language tag for the host environment’s current locale.

这意味着获取默认语言环境取决于实现,可能会有所不同从浏览器到浏览器。这是故意的,因为它允许浏览器供应商保持他们当前的,不同的实现,以保持符合而不用大惊小怪。

This means that getting the default locale is implementation dependent and can differ from browser to browser. This is intentional, as it allows browser vendors to keep their current, differing implementations to stay conforming without much fuss.

虽然确实如此标准化也是不错的,但是首先让每个人加入广泛的规范会更有利然后再解决这些小问题。

While it's true that it would be nice to have this standardized as well, it's more beneficial to get everyone on board for a broad spec first and then work out the little kinks later.

大多数现代浏览器允许您在其偏好设置中更改当前的默认语言环境(显示的是Chrome):

Most modern browsers allow you to change your current default locale in their preferences (Chrome shown):

这篇关于JavaScript如何检测默认语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆