views:

326

answers:

2

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?

+1  A: 

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.

bobince
Thanks for your answer. I asked the question without testing the problem properly first- now I have and it matches what you are saying- so I don't have to worry about it which is perfect! Thanks
DannykPowell
+1  A: 

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).

Álvaro G. Vicario
Thanks for your answer. I decided to select the other answer as the correct one as it was more definitive- but if I could select this as correct also I would- sorry about that
DannykPowell