关于JavaScript的:toLocale 用法

when to, or to not, use toLocale methods?

下面的TLDR

我在读MDN上javascript的标准内置对象部分,注意到有一些方法利用了"locale",特别是根据我所能收集到的,将方法返回的文本格式化为本地定义的格式(如果存在的话)。很明显这会引起土耳其的问题(我不知道还有没有其他的问题)

据我所知,从我的调查来看,这些都是在2011年前后的ES 5.1中实现的。事实上,在下面引用中的一个so链接中,它积极地指出Angular 1.x之所以使用ToString而不是ToLocalString,是因为它与还没有完全采用ES5.1的浏览器向后兼容——简单地说一句:我不知道这是否是事实,但它似乎是合理的。

所以我查阅了ES6规范来检查方法:

关于对象:

15.2.4.3 Object.prototype.toLocaleString ( ) This function returns the result of calling toString(). NOTE This function is provided to
give all Objects a generic toLocaleString interface, even though not
all may use it. Currently, Array, Number, and Date provide their own
locale-sensitive toLocaleString methods. NOTE The first parameter to
this function is likely to be used in a future version of this
standard; it is recommended that implementations do not use this
parameter position for anything else.

数组:

15.4.4.3 Array.prototype.toLocaleString ( )
The elements of the array are converted to strings using their toLocaleString methods, and these strings are
then concatenated, separated by occurrences of a separator string that has been derived in an implementationdefined
locale-specific way. The result of calling this function is intended to be analogous to the result of
toString, except that the result of this function is intended to be locale-specific.

弦上:

15.5.4.17 String.prototype.toLocaleLowerCase ( )
This function works exactly the same as toLowerCase except that its result is intended to yield the correct result
for the host environment’s current locale, rather than a locale-independent result. There will only be a difference in
the few cases (such as Turkish) where the rules for that language conflict with the regular Unicode case mappings.
NOTE The toLocaleLowerCase function is intentionally generic; it does not require that its this value be a String object.
Therefore, it can be transferred to other kinds of objects for use as a method.
NOTE The first parameter to this function is likely to be used in a future version of this standard; it is recommended that
implementations do not use this parameter position for anything else.

为了清晰起见,原版Tolowercase:

15.5.4.16 String.prototype.toLowerCase ( )
If this object is not already a string, it is converted to a string. The characters in that string are converted one by one
to lower case. The result is a string value, not a String object.
The characters are converted one by one. The result of each conversion is the original character, unless that
character has a Unicode lowercase equivalent, in which case the lowercase equivalent is used instead.
NOTE The result should be derived according to the case mappings in the Unicode character database (this explicitly includes
not only the UnicodeData.txt file, but also the SpecialCasings.txt file that accompanies it in Unicode 2.1.8 and later).
NOTE The toLowerCase function is intentionally generic; it does not require that its this value be a String object. Therefore, it
can be transferred to other kinds of objects for use as a method

(toLocalUpperCase/toUpperCase的读数完全相同)

考虑到所有这些,随着ES6的发布和它被大量采用,我感到困惑。我觉得tolowercase和touppercase通常用于验证目的(尽管ES6的情况不那么常见),将它们更改为使用区域设置似乎是错误的,因为您将检查未知格式。所以,好吧,对验证没有真正的帮助。那么用toLocalString输出到dom呢?这似乎是合理的,但同样,你在处理未知。假设您的区域设置没有格式化,并且您希望整数1000显示为"1000"。(我读到过en-gb会发生这种情况)它会让你无法控制,你甚至可能永远不会知道它没有按照你想要的方式显示。

TLDR:对于toLocaleStringtoLocaleLowerCasetoLocaleUpperCase等方法是否有实际的使用案例?它们应该在很大程度上被忽视吗?

注:我知道这是站在固执己见的立场上的,但我认为不是。我在寻找合理的案例,如果它们存在的话,这些案例可能适用。比如问"你会用什么,打电话给"和"你认为"相反,打电话比打电话好。

工具书类

MDN字符串原型:ToLocalLowercase

那么:吐露糖和吐露糖的区别?

那么:到底哪些JS引擎是tolowercase-touppercase语言环境敏感的?

那么:日期的toString和toLocalString方法之间的javascript区别是什么?


It seems plausible, but again, you're dealing with unknowns.

对。你需要了解他们。

It will leave it out of your hands and you may never even know that it's not displaying as you wanted it to.

的确。如果您想要/需要完全控制输出,您需要自己实现格式化。如果你只说,嘿,这是一个数字,请把它格式化成你认为最适合英国地区的格式,然后你就可以使用它了。

Is there a practical use case for methods like toLocaleString etc.?

对!您将希望在支持ECMA-402标准的环境中使用它们。

从API概述:

"EcmaScript 2016国际化API规范提供了大多数应用程序所需的几种关键的语言敏感功能:字符串比较(排序)、数字格式、日期和时间格式以及大小写转换。ECMAScript 2016语言规范为这一基本功能提供了功能(在Array.prototype上:toLocaleString;在String.prototype上:localeComparetoLocaleLowerCasetoLocaleUpperCase;在Number.prototype上:EDOCX1 1〔1〕;在Date.prototype上:EDOCX1 1〔1〕1、EDOCX1 1〔10〕和EDOCX1 1〔11〕提供了这些基本功能的功能功能功能,但它将这些功能的实际行为留给了这些功能的实际行为la la-la-la-la-la-la-la-la-1〔1〔3〕和EDOCX1 1〔4〕和EDOCX1 1〔5〕;在EDOCX1 1〔6〕上:EDOCX1 1〔一个最多可定义个实现。ECMAScript 2016国际化API规范提供了额外的功能、对语言的控制、对要使用的行为的详细信息,以及对所需功能的更完整规范。"

Should they be largely ignored?

可能是在一个未知的环境中。但当你知道他们做什么的时候(因为你控制了环境,或者你期望它符合ECMA-402),就不会这样了,因为在这些情况下,他们会非常有用,并且会让你付出大量的工作。