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?
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.
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).
精彩评论