Globalization API: Objects needed?

Brendan Eich brendan at mozilla.com
Sun Nov 20 09:59:54 PST 2011


On Nov 19, 2011, at 7:27 PM, Norbert Lindenberg wrote:

>>> 2.a. Find out if there were fallbacks through object state
>> 
>> That can be done by object-detecting the proto-methods too, right?
> 
> I'm not sure what you mean - how would you implement the functionality of Collator.resolveOptions without Collator objects?

You wouldn't. I understood "fallbacks through object state" to mean testing (typeof Globalization != "undefined" && Globalization.DateTimeFormat) or equivalent, but testing for a prototype-based method can work as well.

Clearly I misunderstood what "Find out if there were fallbacks through object state" means. Could you give an example?


> 
>>> 2.b. Apply format repeatedly using the same options (internal caching may remove this requirement)
>> 
>> That seems like it could be a valid use-case in its own right, even with caching. Can you show code written both ways that makes realistic re-use of the object?
> 
> The most common use case:
> 
> var collator = new Globalization.Collator(localeList, options);
> myArray.sort(function (x, y) {
>    return collator.compare(x, y);
> });
> 
> This could also be written as:
> 
> myArray.sort(function (x, y) {
>    return x.localeCompare(y, localeList, options);
> });
> 
> but that would involve finding a cached Collator object for localeList and options for every single string comparison, and sorting an array involves many comparisons.
> 
> But there are also applications that format large numbers of numbers or dates in lists and tables, so that reusing an object would help.

Thanks, that all makes sense.

As Alex Russell pointed out, the DOM has default locale information induced by the browser and document. Making localeList an explicit parameter may just be burdening typical JS G11n code with having to acquire and then pass around the same information that (once control flows back into a DOM method that is *not* parameterized that way) is recovered from the default DOM information. If this is true, then it could be worth catering to the default locale(list).

Hope I'm understanding this stuff -- corrections welcome.

/be


More information about the es-discuss mailing list