Reputation: 8498
The ECMAScript Internationalization API makes it possible to localize currency, date, time and other values. Here a little example:
var date = new Date();
alert(new Intl.DateTimeFormat('en-US').format(date));
alert(new Intl.DateTimeFormat('de-CH').format(date));
How is it possible to override the browser default locale, so it's possible to set the locale site wide? Currently I have helpers, which wrap the Intl API and set the local. Is there an easier solution?
Upvotes: 4
Views: 3520
Reputation: 5456
Due to potential security risks and potential interference with embedded apps, you've got the right approach wrapping the API with your own API and there is no standard way of setting a default locale.
Quoting this comprehensive article on the Internationalization API, the author had this to say with regards to exposing an API that allowed setting a default locale:
Two issues prevent this: First, a settable default locale list would create a global communication channel between different scripts running within the same environment, which is considered a security risk. Second, an application may include different components, such as embedded apps, that need different default locales. ECMAScript has no knowledge of these components and no way to manage appropriate contexts for them. We decided therefore that a default locale is better left to higher-level systems. For example, the YUI library already includes an Intl module which manages a list of requested locales that is scoped to the containing YUI object and used for loading resource bundles. This module could easily be modified to keep a locale list object so that it can be used as a default within the scope of the containing YUI object.
Additionally, if you take a look at the official specification, the default locale is implementation-specific. Right now, there is no browser that exposes an API to set the default locale.
Upvotes: 4