JScript's ultimate globalization hero: VBScript

by Michael S. Kaplan, published on 2008/04/29 03:01 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2008/04/29/8436482.aspx


David asked:

Hi,

Does anyone know how to setLocale in Jscript? It seems like JS assumes that everything gets localized in current locale.
Also, how do we convert string “1,234” into double 1234 (taking comma into consideration)

The reason I am asking this is that I am trying to resolve a globalization issue.
I think UK locale tends to have “1.234,56” format for their currency while we have “1,234.56” for currency.
If anyone can knows how this can be resolved would be great.

The problem can easily be solved in VBScript:
--There is a SetLocale function (I just use it to test)
--And Cdbl() converts “12,34” correctly to 12.34 in UK Locale
--Also converts “1,234” correctly to 1234 in US Locale

Wondering if there’s a solution in JScript.

Thank you very much!

Luckily, inestimable developer Paul was there with the best tongue-in-cheek response this month:

You can write your conversion functions in vbscript and call them from jscript.

JScript and JavaScript (aka ECMAScript) all have in common this particular lack of the kind of support for internationalization that people like me consider to be pretty important.

It is kind of fun when the lack is described in terms less likely to lead to diatribes about that lack, which are seldom placed so as to foster a solution.

Paul's answer, on the other hand, in light of the just sipped bit Limonata, just inspires a YOMANK from me. :-)

The situation is fairly unfortunate, given how much more ubiquitous ECMAScript is, especially for client side scripting in the browser, but also in general. But no one seems eager to jump in and see it enhanced at this point....

 

This blog brought to you by , (U+002c, aka COMMA)


jmdesp on 29 Apr 2008 6:53 AM:

Your comment seems to be darkening the js situation more than necessary.

Do you know about toLocaleString ?

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object:toLocaleString

It is a stated objective for Mozilla to enhance the current state of things :

http://www.mozilla.org/projects/intl/locale-model.html

Current implementation suffers mostly on this point "it is necessary to allow the page author to select the locale for scripts and formatting for many instances" and also non-support for dynamic language switching (instantiated by the user) but that's more a browser than a language limitation.

Michael S. Kaplan on 29 Apr 2008 8:44 AM:

A minor number of methods compared to the extensive globalization support built in to the other language? The darkening is well deserved....

Mihai on 29 Apr 2008 12:41 PM:

toLocaleString would be nice, if implement right (in several browsers the result is the same as toString).

Useless.

When it works, it uses the language of the browser's UI, which has nothing to do with the language of the document I am looking at, or the language preferences the user sets for the browser.

Another one: there is no way to query the language preferences, only the browser UI locale.

Ben Cooke on 29 Apr 2008 12:52 PM:

Incidentally, David's initial premise was false: In the UK we use a period as the decimal separator and a comma to separate thousands.

It's true that several countries in Europe do this the other way around, though. It's generally best to assume that whatever Europe does, the UK will do the opposite. :)

Paul Dempsey on 29 Apr 2008 6:57 PM:

Glad to be good for a chuckle (what's a yomank?). Love the Limonata myself.

If all you have is a hammer, go to the hardware store when you need a screwdriver, and don't try to get the hammer to do the job. I was merely suggesting that there are a few more tools available. But then, I tend to explore assumptions, rather than taking it as given that only an hammer-based solution would be acceptable, even when they asked if they could do it with a hammer.

The poster never gave a context for where they were trying to solve this problem. For jscript, even though often likely to be on a web page, it could just as easily be in a wsf script where vbscript is a good candidate for the solution. For web apps, less so if you want portability - which is where life gets much sadder.

Intl support is always a messy thing  to get into. I think it really belongs in a processing module separate from the programming language, and language designers should stay out of it. There should standard objects to do these conversions, available for whatever language you like to use.

cheers,

-- P

Michael S. Kaplan on 29 Apr 2008 7:15 PM:

YOMANK == You Owe Me A New Keyboard. :-)

I agree with you on the separate processing module, though no one seems interested in pursuing that line of thought....

Bulletmagnet on 30 Apr 2008 6:57 AM:

I think the technical term is

C | N > K


Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

referenced by

2010/11/23 Perhaps not evil, but certainly getting hella snarky

2010/06/16 JavaScript's got a whole new ultimate globalization hero

go to newer or older post, or back to index or month or day