Gadgets.i18n (v0.9)
NeedsExamples
JavaScript Internationalization (i18n)
A container MUST emit the OpenSocial JavaScript internationalization libraries and data files required by the libraries if an application requires the feature
<Require feature="opensocial-i18n"/>
. The container SHOULD emit the OpenSocial JavaScript internationalization libraries and data files required by the libraries if an application optionallly requests the feature
<Optional feature="opensocial-i18n"/>
.
Methods
gadgets.i18n.formatDateTime
;<static> string gadgets.i18n.formatDateTime(pattern, date)
Parameters
NameTypeDescription |
pattern |
string/number |
String to specify patterns or Number used to reference predefined pattern that a date should be formatted into. |
date |
Date |
Date object being formatted. |
Returns
TypeDescription |
string |
string representation of date/time. |
Description
This method formats a "Date" object with provided pattern specification. The pattern could be a string using ICU notation or a predefined pattern. A string using ICU notation offers the most flexibility. Each field as specified in the pattern has locale specific behavior. The pattern string is allowed to contain string literals, and one type of pattern might not work for all locales. In those case, the pattern itself could also be locale specific, thus not good for sharing among locales.
date = new Date(2006, 6, 27, 13, 10, 10, 250);
assertEquals("13:10:10", gadgets.i18n.formatDateTime("HH:mm:ss", date));
gadgets.i18n.formatNumber
;<static> string gadgets.i18n.formatNumber(pattern, value, opt_currencyCode)
Parameters
NameTypeDescription |
pattern |
string/number |
String to specify patterns or Number used to reference predefined pattern that a number should be formatted into. |
value |
number |
The number being formatted. |
opt_currencyCode |
string |
optional international currency code, it determines the currency code/symbol should be used in format/parse. If not given, the currency code for current locale will be used. |
Returns
TypeDescription |
string |
The formatted string. |
Description
Format number using the pattern specified. The pattern could be a string pattern or one of the predefined patterns. The formatted string is returned. If an error is encountered, zero will be returned.
var str = gadgets.i18n.formatNumber("#,###", 1234567890);
assertEquals("1,234,567,890", str);
var str = gadgets.i18n.formatNumber(gadgets.i18n.CURRRENCY_PATTERN, 1234.569);
assertEquals("$1,234.58", str);
gadgets.i18n.parseDateTime
;<static> number gadgets.i18n.parseDateTime(pattern, text, start, date)
Parameters
NameTypeDescription |
pattern |
string/number |
String to specify patterns or Number used to reference predefined pattern that a date should be parsed from. |
text |
string |
The string that need to be parsed. |
start |
number |
The character position in "text" where parse begins. |
date |
Date |
The date object that will hold parsed value. |
Returns
TypeDescription |
number |
The number of characters advanced or 0 if failed. |
Description
This method will parse the input string ("text"), interpretting it as specified by pattern. The parsed result will be saved into a Date object ("date"). "start" indicates from where in the string the parse should start. This method returns the number of characters consumed.
//assume locale has already been set to zh_CN
var date = new Date();
gadgets.i18n.parseDateTime(gadgets.i18n.LONG_DATE_FORMAT, "2006年7月24日", 0, date);
assertEquals(date.getFullYear(), 2006);
assertEquals(date.getMonth(), 7 - 1);
assertEquals(date.getDate(), 24);
gadgets.i18n.parseNumber
;<static> number gadgets.i18n.parseNumber(pattern, text, opt_pos, opt_currencyCode)
Parameters
NameTypeDescription |
pattern |
string/number |
String to specify patterns or Number used to reference predefined pattern that a number should be parsed from. |
text |
string |
input text being parsed. |
opt_pos |
Array |
optional one element array that holds position information. It tells from where parse should begin. Upon return, it holds parse stop position. |
opt_currencyCode |
string |
optional international currency code, it determines the currency code/symbol should be used in format/parse. If not given, the currency code for current locale will be used. |
Returns
TypeDescription |
number |
Parsed number, 0 if in error. |
Description
Parse string to get a number the pattern specified. The pattern could be a string pattern or one of the predefined patterns.
var value = gadgets.i18n.parseNumber("0E0", "1.2345E+4");
assertEquals(12345.0, value); //assume locale has already been set to fr
var value = gadgets.i18n.parseNumber(gadgets.i18n.CURRENCY_PATTERN, "0,30 €");
assertEquals(0.30, value);
JavaScript Internationalization (i18n)
A container MUST emit the OpenSocial JavaScript internationalization libraries and data files required by the libraries if an application requires the feature <Require feature="opensocial-i18n"/>. The container SHOULD emit the OpenSocial JavaScript internationalization libraries and data files required by the libraries if an application optionallly requests the feature
<Optional feature="opensocial-i18n"/>
.
gadgets.i18n.CURRENCY_PATTERN
Pattern for currency.
gadgets.i18n.DECIMAL_PATTERN
Pattern for decimal numbers.
gadgets.i18n.FULL_DATE_FORMAT
Format for full representations of dates.
gadgets.i18n.FULL_DATETIME_FORMAT
Format for short representations of datetimes.
gadgets.i18n.FULL_TIME_FORMAT
Format for full representations of times.
gadgets.i18n.LONG_DATE_FORMAT
Format for long representations of dates.
gadgets.i18n.LONG_DATETIME_FORMAT
Format for short representations of datetimes.
gadgets.i18n.LONG_TIME_FORMAT
Format for long representations of times.
gadgets.i18n.MEDIUM_DATE_FORMAT
Format for medium representations of dates.
gadgets.i18n.MEDIUM_DATETIME_FORMAT
Format for medium representations of datetimes.
gadgets.i18n.MEDIUM_TIME_FORMAT
Format for medium representations of times.
gadgets.i18n.PERCENT_PATTERN
Pattern for percentages.
gadgets.i18n.SCIENTIFIC_PATTERN
Pattern for scientific numbers.
gadgets.i18n.SHORT_DATE_FORMAT
Format for short representations of dates.
gadgets.i18n.SHORT_DATETIME_FORMAT
Format for short representations of datetimes.
gadgets.i18n.SHORT_TIME_FORMAT
Format for short representations of times.
{{ JsApiAlphaList_(v0.9) }}