Globalization

Globalization

Obtains information and performs operations specific to the user's locale and timezone.

Objects

Methods

Variable Scope

The globalization object is a child of the navigator object, and therefore has global scope.

// The global globalization object
var globalization = navigator.globalization;

Permissions

Android

app/res/xml/config.xml

<plugin name="Globalization" value="org.apache.cordova.Globalization" />

globalization.getPreferredLanguage

Get the string identifier for the client's current language.

navigator.globalization.getPreferredLanguage(successCallback, errorCallback);

Description

Returns the language identifier string to the successCallback with a properties object as a parameter. That object should have a value property with a String value.

If there is an error getting the language, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.UNKNOWN\_ERROR.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this should display a popup dialog with the text language: English:

navigator.globalization.getPreferredLanguage(
    function (language) {alert('language: ' + language.value + '\n');},
    function () {alert('Error getting language\n');}
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getPreferredLanguage Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkLanguage() {
      navigator.globalization.getPreferredLanguage(
        function (language) {alert('language: ' + language.value + '\n');},
        function () {alert('Error getting language\n');}
      );
    }
    </script>
  </head>
  <body>
    <button onclick="checkLanguage()">Click for language</button>
  </body>
</html>

Windows Phone 8 Quirks


globalization.getLocaleName

Get the string identifier for the client's current locale setting.

navigator.globalization.getLocaleName(successCallback, errorCallback);

Description

Returns the locale identifier string to the successCallback with a properties object as a parameter. That object should have a value property with a String value.

If there is an error getting the locale, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.UNKNOWN\_ERROR.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this displays a popup dialog with the text locale: en\_US.

navigator.globalization.getLocaleName(
    function (locale) {alert('locale: ' + locale.value + '\n');},
    function () {alert('Error getting locale\n');}
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getLocaleName Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkLocale() {
      navigator.globalization.getLocaleName(
        function (locale) {alert('locale: ' + locale.value + '\n');},
        function () {alert('Error getting locale\n');}
      );
    }
    </script>
  </head>
  <body>
    <button onclick="checkLocale()">Click for locale</button>
  </body>
</html>

Windows Phone 8 Quirks


globalization.dateToString

Returns a date formatted as a string according to the client's locale and timezone.

navigator.globalization.dateToString(date, successCallback, errorCallback, options);

Description

Returns the formatted date String via a value property accessible from the object passed as a parameter to the successCallback.

The inbound date parameter should be of type Date.

If there is an error formatting the date, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.FORMATTING\_ERROR.

The options parameter is optional, and its default values are:

{formatLength:'short', selector:'date and time'}

The options.formatLength can be short, medium, long, or full.

The options.selector can be date, time or date and time.

Supported Platforms

Quick Example

If the browser is set to the en\_US locale, this displays a popup dialog with text similar to date: 9/25/2012 4:21PM using the default options:

navigator.globalization.dateToString(
    new Date(),
    function (date) { alert('date: ' + date.value + '\n'); },
    function () { alert('Error getting dateString\n'); },
    { formatLength: 'short', selector: 'date and time' }
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>dateToString Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkDateString() {
      navigator.globalization.dateToString(
        new Date(),
        function (date) {alert('date: ' + date.value + '\n');},
        function () {alert('Error getting dateString\n');,
        {formatLength:'short', selector:'date and time'}}
      );
    }
    </script>
  </head>
  <body>
    <button onclick="checkDateString()">Click for date string</button>
  </body>
</html>

Windows Phone 8 Quirks


globalization.stringToDate

Parses a date formatted as a string, according to the client's user preferences and calendar using the time zone of the client, and returns the corresponding date object.

navigator.globalization.stringToDate(dateString, successCallback, errorCallback, options);

Description

Returns the date to the success callback with a properties object as a parameter. That object should have the following properties:

The inbound dateString parameter should be of type String.

The options parameter is optional, and defaults to the following values:

{formatLength:'short', selector:'date and time'}

The options.formatLength can be short, medium, long, or full. The options.selector can be date, time or date and time.

If there is an error parsing the date string, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.PARSING\_ERROR.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this displays a popup dialog with text similar to month:8 day:25 year:2012. Note that the month integer is one less than the string, as the month integer represents an array index.

navigator.globalization.stringToDate(
    '9/25/2012',
    function (date) {alert('month:' + date.month +
                           ' day:'  + date.day   +
                           ' year:' + date.year  + '\n');},
    function () {alert('Error getting date\n');},
    {selector: 'date'}
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>stringToDate Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkStringDate() {
      navigator.globalization.stringToDate(
        '9/25/2012',
        function (date) {alert('month:' + date.month +
                               ' day:' + date.day +
                               ' year:' + date.year + '\n');},
        function () {alert('Error getting date\n');},
        {selector:'date'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkStringDate()">Click for parsed date</button>
  </body>
</html>

Windows Phone 8 Quirks


globalization.getDatePattern

Returns a pattern string to format and parse dates according to the client's user preferences.

navigator.globalization.getDatePattern(successCallback, errorCallback, options);

Description

Returns the pattern to the successCallback. The object passed in as a parameter contains the following properties:

If there is an error obtaining the pattern, the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.PATTERN\_ERROR.

The options parameter is optional, and defaults to the following values:

{formatLength:'short', selector:'date and time'}

The options.formatLength can be short, medium, long, or full. The options.selector can be date, time or date and time.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this example displays a popup dialog with text such as pattern: M/d/yyyy h:mm a:

function checkDatePattern() {
    navigator.globalization.getDatePattern(
        function (date) { alert('pattern: ' + date.pattern + '\n'); },
        function () { alert('Error getting pattern\n'); },
        { formatLength: 'short', selector: 'date and time' }
    );
}

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getDatePattern Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkDatePattern() {
      navigator.globalization.getDatePattern(
        function (date) {alert('pattern: ' + date.pattern + '\n');},
        function () {alert('Error getting pattern\n');},
        {formatLength:'short', selector:'date and time'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkDatePattern()">Click for pattern</button>
  </body>
</html>

Windows Phone 8 Quirks


globalization.getDateNames

Returns an array of the names of the months or days of the week, depending on the client's user preferences and calendar.

navigator.globalization.getDateNames(successCallback, errorCallback, options);

Description

Returns the array of names to the successCallback with a properties object as a parameter. That object contains a value property with an Array of String values. The array features names starting from either the first month in the year or the first day of the week, depending on the option selected.

If there is an error obtaining the names, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.UNKNOWN\_ERROR.

The options parameter is optional, and its default values are:

{type:'wide', item:'months'}

The value of options.type can be narrow or wide.

The value of options.item can be months or days.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this example displays a series of twelve popup dialogs, one per month, with text similar to month: January:

navigator.globalization.getDateNames(
    function (names) {
        for (var i = 0; i < names.value.length; i++) {
            alert('month: ' + names.value[i] + '\n');
        }
    },
    function () { alert('Error getting names\n'); },
    { type: 'wide', item: 'months' }
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getDateNames Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkDateNames() {
      navigator.globalization.getDateNames(
        function (names) {
          for (var i=0; i<names.value.length; i++) {
            alert('month: ' + names.value[i] + '\n');
          }
        },
        function () {alert('Error getting names\n');},
        {type:'wide', item:'months'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkDateNames()">Click for date names</button>
  </body>
</html>

globalization.isDayLightSavingsTime

Indicates whether daylight savings time is in effect for a given date using the client's time zone and calendar.

navigator.globalization.isDayLightSavingsTime(date, successCallback, errorCallback);

Description

Indicates whether or not daylight savings time is in effect to the successCallback with a properties object as a parameter. That object should have a dst property with a Boolean value. A true value indicates that daylight savings time is in effect for the given date, and false indicates that it is not.

The inbound parameter date should be of type Date.

If there is an error reading the date, then the errorCallback executes. The error's expected code is GlobalizationError.UNKNOWN\_ERROR.

Supported Platforms

Quick Example

During the summer, and if the browser is set to a DST-enabled timezone, this should display a popup dialog with text similar to dst: true:

navigator.globalization.isDayLightSavingsTime(
    new Date(),
    function (date) {alert('dst: ' + date.dst + '\n');},
    function () {alert('Error getting names\n');}
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>isDayLightSavingsTime Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkDayLightSavings() {
      navigator.globalization.isDayLightSavingsTime(
        new Date(),
        function (date) {alert('dst: ' + date.dst + '\n');},
        function () {alert('Error getting names\n');}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkDayLightSavings()">Click for daylight savings</button>
  </body>
</html>

globalization.getFirstDayOfWeek

Returns the first day of the week according to the client's user preferences and calendar.

navigator.globalization.getFirstDayOfWeek(successCallback, errorCallback);

Description

The days of the week are numbered starting from 1, where 1 is assumed to be Sunday. Returns the day to the successCallback with a properties object as a parameter. That object should have a value property with a Number value.

If there is an error obtaining the pattern, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.UNKNOWN\_ERROR.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this displays a popup dialog with text similar to day: 1.

navigator.globalization.getFirstDayOfWeek(
    function (day) {alert('day: ' + day.value + '\n');},
    function () {alert('Error getting day\n');}
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getFirstDayOfWeek Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkFirstDay() {
      navigator.globalization.getFirstDayOfWeek(
        function (day) {alert('day: ' + day.value + '\n');},
        function () {alert('Error getting day\n');}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkFirstDay()">Click for first day of week</button>
  </body>
</html>

globalization.numberToString

Returns a number formatted as a string according to the client's user preferences.

navigator.globalization.numberToString(number, successCallback, errorCallback, options);

Description

Returns the formatted number string to the successCallback with a properties object as a parameter. That object should have a value property with a String value.

If there is an error formatting the number, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.FORMATTING\_ERROR.

The options parameter is optional, and its default values are:

{type:'decimal'}

The options.type can be 'decimal', 'percent', or 'currency'.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this displays a popup dialog with text similar to number: 3.142:

navigator.globalization.numberToString(
    3.1415926,
    function (number) {alert('number: ' + number.value + '\n');},
    function () {alert('Error getting number\n');},
    {type:'decimal'}
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>numberToString Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkNumber() {
      navigator.globalization.numberToString(
        3.1415926,
        function (number) {alert('number: ' + number.value + '\n');},
        function () {alert('Error getting number\n');},
        {type:'decimal'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkNumber()">Click for number</button>
  </body>
</html>

globalization.stringToNumber

Parses a number formatted as a string according to the client's user preferences and returns the corresponding number.

navigator.globalization.stringToNumber(string, successCallback, errorCallback, options);

Description

Returns the number to the successCallback with a properties object as a parameter. That object should have a value property with a Number value.

If there is an error parsing the number string, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.PARSING\_ERROR.

The options parameter is optional, and defaults to the following values:

{type:'decimal'}

The options.type can be decimal, percent, or currency.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this should display a popup dialog with text similar to number: 1234.56:

navigator.globalization.stringToNumber(
    '1234.56',
    function (number) {alert('number: ' + number.value + '\n');},
    function () {alert('Error getting number\n');},
    {type:'decimal'}
);

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>stringToNumber Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkNumber() {
      navigator.globalization.stringToNumber(
        '1234.56',
        function (number) {alert('number: ' + number.value + '\n');},
        function () {alert('Error getting number\n');},
        {type:'decimal'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkNumber()">Click for parsed number</button>
  </body>
</html>

globalization.getNumberPattern

Returns a pattern string to format and parse numbers according to the client's user preferences.

navigator.globalization.getNumberPattern(successCallback, errorCallback, options);

Description

Returns the pattern to the successCallback with a properties object as a parameter. That object contains the following properties:

If there is an error obtaining the pattern, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.PATTERN\_ERROR.

The options parameter is optional, and default values are:

{type:'decimal'}

The options.type can be decimal, percent, or currency.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale, this should display a popup dialog with text similar to the results that follow:

navigator.globalization.getNumberPattern(
    function (pattern) {alert('pattern: '  + pattern.pattern  + '\n' +
                              'symbol: '   + pattern.symbol   + '\n' +
                              'fraction: ' + pattern.fraction + '\n' +
                              'rounding: ' + pattern.rounding + '\n' +
                              'positive: ' + pattern.positive + '\n' +
                              'negative: ' + pattern.negative + '\n' +
                              'decimal: '  + pattern.decimal  + '\n' +
                              'grouping: ' + pattern.grouping);},
    function () {alert('Error getting pattern\n');},
    {type:'decimal'}
);

Results:

pattern: #,##0.###
symbol: .
fraction: 0
rounding: 0
positive:
negative: -
decimal: .
grouping: ,

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getNumberPattern Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkPattern() {
      navigator.globalization.getNumberPattern(
        function (pattern) {alert('pattern: '  + pattern.pattern  + '\n' +
                                  'symbol: '   + pattern.symbol   + '\n' +
                                  'fraction: ' + pattern.fraction + '\n' +
                                  'rounding: ' + pattern.rounding + '\n' +
                                  'positive: ' + pattern.positive + '\n' +
                                  'negative: ' + pattern.negative + '\n' +
                                  'decimal: '  + pattern.decimal  + '\n' +
                                  'grouping: ' + pattern.grouping);},
        function () {alert('Error getting pattern\n');},
        {type:'decimal'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkPattern()">Click for pattern</button>
  </body>
</html>

Windows Phone 8 Quirks


globalization.getCurrencyPattern

Returns a pattern string to format and parse currency values according to the client's user preferences and ISO 4217 currency code.

 navigator.globalization.getCurrencyPattern(currencyCode, successCallback, errorCallback);

Description

Returns the pattern to the successCallback with a properties object as a parameter. That object should contain the following properties:

The inbound currencyCode parameter should be a String of one of the ISO 4217 currency codes, for example 'USD'.

If there is an error obtaining the pattern, then the errorCallback executes with a GlobalizationError object as a parameter. The error's expected code is GlobalizationError.FORMATTING\_ERROR.

Supported Platforms

Quick Example

When the browser is set to the en\_US locale and the selected currency is United States Dollars, this example displays a popup dialog with text similar to the results that follow:

navigator.globalization.getCurrencyPattern(
    'USD',
    function (pattern) {
        alert('pattern: '  + pattern.pattern  + '\n' +
              'code: '     + pattern.code     + '\n' +
              'fraction: ' + pattern.fraction + '\n' +
              'rounding: ' + pattern.rounding + '\n' +
              'decimal: '  + pattern.decimal  + '\n' +
              'grouping: ' + pattern.grouping);
    },
    function () { alert('Error getting pattern\n'); }
);

Expected result:

pattern: $#,##0.##;($#,##0.##)
code: USD
fraction: 2
rounding: 0
decimal: .
grouping: ,

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getCurrencyPattern Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkPattern() {
      navigator.globalization.getCurrencyPattern(
        'USD',
        function (pattern) {alert('pattern: '  + pattern.pattern  + '\n' +
                                  'code: '     + pattern.code     + '\n' +
                                  'fraction: ' + pattern.fraction + '\n' +
                                  'rounding: ' + pattern.rounding + '\n' +
                                  'decimal: '  + pattern.decimal  + '\n' +
                                  'grouping: ' + pattern.grouping);},
        function () {alert('Error getting pattern\n');}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkPattern()">Click for pattern</button>
  </body>
</html>

GlobalizationError

An object representing a error from the Globalization API.

Properties

Description

This object is created and populated by Cordova, and returned to a callback in the case of an error.

Supported Platforms

Quick Example

When the following error callback executes, it displays a popup dialog with the text similar to code: 3 and message:

function errorCallback(error) {
    alert('code: ' + error.code + '\n' +
          'message: ' + error.message + '\n');
};

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>GlobalizationError Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova-x.x.x.js"></script>
    <script type="text/javascript" charset="utf-8">

    function successCallback(date) {
      alert('month:' + date.month +
            ' day:' + date.day +
            ' year:' + date.year + '\n');
    }

    function errorCallback(error) {
      alert('code: ' + error.code + '\n' +
            'message: ' + error.message + '\n');
    };

    function checkError() {
      navigator.globalization.stringToDate(
        'notADate',
        successCallback,
        errorCallback,
        {selector:'foobar'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkError()">Click for error</button>
  </body>
</html>