JSignage:Localization
From SpinetiX Support Wiki
Introduction
jSignage adds support for locale-dependent display of date, time, numbers, and currencies using a subset of the Unicode Common Locale Data Repository data base.
A locale is identified by its Unicode tag, which is a string composed of two or three parts: a language subtag and a region subtag, plus an optional script subtag - for example, en_US
for US English (default locale), zh_Hans_CN
for mainland China, and es_MX
for the Mexican locale. For more details, see the list of supported locale tags.
The default locale can be obtained by a call to $.getLocale()
, and changed by a call to $.setLocale( tag )
.
The Localization API is based on two class-functions:
-
$.DateTimeFormat
for the formatting of date and time -
$.NumberFormat
for the formatting of numbers and currencies
.DateTimeFormat()
$.DateTimeFormat( format, locale);
Returns: Function
Description
Class for the formatting of date and time. To use it, create an object by calling the class constructor with a format selector and an optional locale tag (the default locale is used if not provided).
Parameters
-
format
- Type: Number or String. (required)
- The format selector for date and time is either one of the predefined numeral formats (such as
$.DateTimeFormat.Format.FULL_DATETIME
) or a pattern string (such as "EEEE, MMMM d, y
" which is expanded to "Wednesday, July 10, 1996
" for en_US) - for more details see Date and time formats.
-
locale
- Type: String.
- Optional locale code tag.
.NumberFormat()
$.NumberFormat( format, locale);
Returns: Function
Description
Class for the formatting of numbers and currencies. To use it, create an object by calling the class constructor with a format selector and an optional locale tag (the default locale is used if not provided).
Parameters
-
format
- Type: Number or String. (required)
- The format selector for numbers and currencies is either one of the predefined formats (such as
$.NumberFormat.Format.DECIMAL
) or a pattern string (such as "#.#####"
" which specifies that the number can have up to 5 decimals) - for more details see Number formats.
-
locale
- Type: String.
- Optional locale code tag.
.getLocale()
$.getLocale();
Returns: String
This function gets the default locale. It doesn't have any parameters.
.setLocale()
$.setLocale( locale );
Returns: String
This function sets the default locale; it also returns the previous default locale. It takes one parameter:
-
locale
- Type: String.
- Locale code tag.
Examples
// formatting date, time and numbers
$.setLocale( 'es_ES' ); // sets the default locale
f = new $.DateTimeFormat( $.DateTimeFormat.Format.LONG_DATE );
str = f.format( new Date() ); // -> "24 de abril de 2013"
f = new $.DateTimeFormat( "dd/MM/yyyy" );
str = f.format( new Date() ); // -> "24/04/2013"
f = new $.DateTimeFormat( $.DateTimeFormat.Format.LONG_DATE, 'zh_Hans_CN' );
str = f.format( new Date() ); // -> "2013年4月24日"
f = new $.NumberFormat( $.NumberFormat.Format.DECIMAL );
str = f.format( Math.PI ); // -> "3.142"
f = new $.NumberFormat( "#.#####" );
str = f.format( Math.PI ); // -> "3.14159"