User:Waldo/Internationalization API: Difference between revisions

Talk some about number formatting
(Some more organization work)
(Talk some about number formatting)
Line 3: Line 3:
ECMAScript has long had rudimentary localization support.  ES5 defines <code>toLocaleString</code> methods (found on various objects like <code>Array.prototype</code>, <code>Number.prototype</code>, and <code>Date.prototype</code>); <code>toLocaleLowerCase</code> and <code>toLocaleUpperCase</code> on <code>String.prototype</code>; and <code>toLocaleDateString</code> and <code>toLocaleTimeString</code> on <code>Date.prototype</code>.  Each method acts only with respect to the user's current locale, and each method provides no control over output formatting.  The spec algorithms are woefully under-defined.  As a practical matter localization support in ES5 is useless.
ECMAScript has long had rudimentary localization support.  ES5 defines <code>toLocaleString</code> methods (found on various objects like <code>Array.prototype</code>, <code>Number.prototype</code>, and <code>Date.prototype</code>); <code>toLocaleLowerCase</code> and <code>toLocaleUpperCase</code> on <code>String.prototype</code>; and <code>toLocaleDateString</code> and <code>toLocaleTimeString</code> on <code>Date.prototype</code>.  Each method acts only with respect to the user's current locale, and each method provides no control over output formatting.  The spec algorithms are woefully under-defined.  As a practical matter localization support in ES5 is useless.


The ECMAScript Internationalization API (ECMA-402) significantly extends these capabilities, to provide genuinely useful means of localization to ECMAScript.  Outputs may be customized in various ways by requesting different components be included in output, formatted in various ways.  The locale used for a formatting operation is customizable, and output formatting is intelligently determined in accordance with the locale.  It additionally provides means of locale-sensitively sorting data, according to the type of that data (for example, sorting names in phone book order, versus sorting them in dictionary order), considering or ignoring capitalization, accents, and so on.
The ECMAScript Internationalization API (ECMA-402) significantly extends these capabilities, to provide genuinely useful means of localization to ECMAScript.  Outputs may be customized in various ways by requesting different components be included in output, formatted in various ways.  The locale used for a formatting operation is customizable, and output formatting is intelligently determined in accordance with the locale.  It additionally provides comparison functions useful for locale-sensitively sorting data, according to the type of that data (for example, sorting names in phone book order, versus sorting them in dictionary order), considering or ignoring capitalization, accents, and so on.


The Internationalization API introduces one new global property: <code>Intl</code>.  This property is an object with various properties corresponding to various sub-APIs: collation (sorting), number formatting, and date/time formatting.  (More capabilities will be added in future Internationalization API updates.)  The localization APIs from ES5 have been reformulated to use the localization capabilities provided by the Internationalization API.  Generally, however, it's preferable to use the Internationalization API directly, as this is more efficient by permitting caching of the structures needed to perform each operation.
The Internationalization API introduces one new global property: <code>Intl</code>.  This property is an object with various properties corresponding to various sub-APIs: collation (sorting), number formatting, and date/time formatting.  (More capabilities will be added in future Internationalization API updates.)  The localization APIs from ES5 have been reformulated to use the localization capabilities provided by the Internationalization API.  Generally, however, it's preferable to use the Internationalization API directly, as this is more efficient by permitting caching of the structures needed to perform each operation.
Line 23: Line 23:
=== Currency codes ===
=== Currency codes ===


Formatting a number to display as currency depends upon the particular currency used, so currency codes play a role in number formatting.  For example, one correct formatting for one hundred dollars USD is "$1.00" (two decimal places), while one hundred Japanese yen would be "¥100" (no decimal places).  Additional characteristics determined by currency, besides decimal place count, include the currency symbol and a "long" name for it (e.g. dollars or yen).  Currency codes are three letters traditionally capitalized.  The full list is found in ISO 4217; the list is also available [http://www.xe.com/iso4217.php elsewhere].
Formatting a number to display as currency depends upon the particular currency used, so currency codes play a role in number formatting.  For example, one correct formatting for one hundred dollars USD is "$1.00" (two decimal places), while one hundred Japanese yen would be "¥100" (no decimal places).  Additional characteristics determined by currency, besides decimal place count, include the currency symbol and a "long" name ("US dollar" and so on).  Currency codes are three letters, traditionally capitalized.  The full list is found in ISO 4217 and [http://www.xe.com/iso4217.php elsewhere].


=== Time zone name ===
=== Time zone name ===
Line 31: Line 31:
== Operations ==
== Operations ==


ECMA-402 in its first iteration exposes various locale-sensitive operations.
ECMA-402 in its first iteration exposes various locale-sensitive operations. Future editions will likely expose more operations.
 
...talk about collators, date formats, and the remaining stuff...copiously link to BCP47...


=== Collation ===
=== Collation ===


Collation is the process of sorting a list of strings, according to particular rules.  Different locales sort text differently.  Locales may also sort differently in different contexts: dictionary sort order versus phonebook sort order, say.  Sorting also may or may not take into account numeric value: <code><nowiki>[</nowiki>1, 30, 5<nowiki>]</nowiki> order versus <code><nowiki>[</nowiki>1, 5, 30<nowiki>]</nowiki></code> order.
Collation is the process of sorting a list of strings, according to particular rules.  Different locales sort text differently.  Locales may also sort differently in different contexts: dictionary sort order versus phonebook sort order, say.  Sorting also may or may not take into account numeric value: <code><nowiki>[</nowiki>1, 30, 5<nowiki>]</nowiki> order versus <code><nowiki>[</nowiki>1, 5, 30<nowiki>]</nowiki></code> order.
=== Number formatting ===
Number formatting is simple if one only wishes for a decimal format.  But in many contexts, simple decimal formatting is undesirable.  Numbers displayed as currencies, percents, and decimal will display differently in different locales.  Currencies pose additional problems, as different currencies format fractional values to different numbers of decimal places.  And for sufficiently large numbers, grouping separators (in en-US, at thousands places as a comma; in many European locales, at thousands places as periods; other, more exotic forms exist) may be desirable.  ECMA-402 permits customization of all these formatting choices.
=== Date formatting ===
Locale-sensitive date formatting in ES5 admits only a single implementation-defined format with a fixed set of components.  ECMA-402 enhances date formatting to allow the selection of components to be customized -- month, day, and year, for example..  The way in which these components will be displayed in the final format string is locale-dependent, as different locales write out dates in different ways.  (For example, an date might be formatted as "September 24, 2012" for en-US and as "24 Sept. 2012" for fr-FR.)  Moreover, various styles may be chosen for the components included in the format: "narrow", "short", "long", "2-digit", and "numeric".  (Exactly what these styles look like is also implementation-dependent.)  These styles feed into the final computation of an appropriate pattern to use to generate a final string.


== Internationalization in SpiderMonkey ==
== Internationalization in SpiderMonkey ==
Confirmed users
446

edits