相当于 vbscript setlocale 的 Javascript

发布于 2024-08-17 18:57:37 字数 358 浏览 4 评论 0原文

希望有人能快速回答,但我的谷歌福已经抛弃了我。

在 vbscript 中,我可以使用“setlocale”来设置脚本运行的区域设置。我需要一个 javascript 版本。

例如,假设我的用户使用的是法语设置的浏览器,但我希望数字和日期为英语,并且可以像英语一样使用。然后在 vbscript 中我可以

setlocale 2057 

设置为英语区域设置

,然后当我完成数字/日期的处理后,我可以设置回法语

setlocale 1036

是否有等效的 JavaScript?我怎样才能在JavaScript中完成这个?

Hopefully a quick answer for someone, but my google-fu has deserted me.

In vbscript I can use "setlocale" to set the locale a script is running in. I need a javascript version.

So for example, say my user is using a browser with french settings, but I want numbers and dates to be in english and worked with as if they are english. Then in vbscript I could do

setlocale 2057 

to set to english locale

and then when I'm finished working with the numbers/ dates I can set back to french

setlocale 1036

Is there a javascript equivalent? How can I accomplish this in javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

韶华倾负 2024-08-24 18:57:37

不,无法在 JavaScript 中选择区域设置。

然而,在美国英语语言环境中执行操作通常不是问题,因为 JavaScript 就是这么做的。如果您使用 parseInt、toString 等处理 Number ,您将始终使用 . 来处理小数点,并且不会有千位分隔符。 Date 默认格式还使用英语工作日和月份名称,以及浏览器特定的日期格式,该格式会忽略您的区域设置的默认值。

您必须使用像 toLocaleString 这样的方法,不遗余力地在 JavaScript 中获取任何特定于语言环境的内容。跨浏览器的结果通常很差且不一致。如果您希望在网络应用程序中实现可靠的本地化,则必须自己完成。

No, there's no way to choose locales in JavaScript.

However, doing stuff in the US English locale is not generally a problem since that's what JavaScript does anyway. If you handle Number​s with parseInt, toString and so on you will always be dealing with . for decimal points and there will be no thousands-separator. Date default formatting also uses English weekday and month names, and a browser-specific date format that ignores what your locale's default would be.

You have to go out of your way to get anything locale-specific in JavaScript, with methods like toLocaleString. The results are typically poor and inconsistent across browsers. If you want reliable localisation in your webapps, you have to do it yourself.

短暂陪伴 2024-08-24 18:57:37

据我所知,JavaScript 不提供类似的语言环境功能。您有一些用于特定任务的区域设置感知函数。例如,您可以使用 localeCompare() 对字符串进行排序,您可以将其大写toLocaleUpperCase() 并且您可以使用 toLocaleString() 打印日期。而且您不指定区域设置,它们会使用解释器认为合适的任何语言设置。当然,这完全取决于所有目标浏览器都提供这些功能。

您可以在 ECMAScript 站点下载官方规范,并查看Mozilla 实现(IE 实现可能位于 MSDN 站点中的某个位置)。

As far as I know, JavaScript does not offer similar locale features. You have some locale-aware functions for specific tasks. For instance, you can sort a string with localeCompare(), you can uppercase it with toLocaleUpperCase() and you can print a date with toLocaleString(). And you don't specify a locale, they work with whatever language settings the interpreter thinks appropriate. And, of course, it all depends on having these functions available in all target browsers.

You can download the official specs at the ECMAScript site and have a look at the Mozilla implementation (the IE implementation is probably somewhere in the MSDN site).

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