Blog RSS Feed

Migrating from the Globalization Plugin
20 Nov 2017

Migrating from the Cordova Globalization Plugin

The Cordova Globalization Plugin was created to obtain information and perform operations based on a user’s locale, language and timezone when most mobile platforms could not make a distinction between these settings. With the new API arrivals in the browser, we can now use the ECMA Internationalization API for achieving this goal on iOS, Android, Windows devices and desktop browsers. Hence, this cordova plugin is not needed any more and will be sunset soon.

Migrating from the plugin to the Internationalization API

The cordova globalization plugin defines a global navigator.globalization object which provides various methods to access a user’s locale, language and timezone. To get the preferred language from the browser, the navigator.globalization.getPreferredLanguage method was used as shown below:

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

The current locale could be found out using:

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

The ECMA Internationalization API provides the Intl object which provides language sensitive string comparison, number formatting, and date and time formatting.  First we should check if the API is supported by the browser:

if (window.Intl && typeof window.Intl === 'object') {
    console.log('API available');
}

The preferred language tag can be found out from the browser by using the navigator object:

console.log(navigator.language);

The locale name can be found out using the Intl.getCanonicalLocales(locales) method. locales is a string value or an array of string values that has the language tag(s). The locale name can then be obtained as shown below:

Intl.getCanonicalLocales('EN-US'); // ["en-US"]
Intl.getCanonicalLocales(['EN-US', 'Fr']); // ["en-US", "fr"]

Another instance of migrating from the cordova globalization plugin can be seen in this example: the navigator.globalization.dateToString method. This method is used in the cordova plugin as shown below:

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

Similar results can be obtained using the Internationalization API by using the following code:

var date = new Date();
console.log(new Intl.DateTimeFormat().format(date));

Here is a good resource to find out more about various methods in the ECMA Internationalization API.

Your feedback is graciously accepted and appreciated!

Previous

Plugins Release
10 Nov 2017 - By Steve Gill

The following plugins were updated today: cordova-plugin-battery-status@1.2.5 cordova-plugin-camera@3.0.0 cordova-plugin-contacts@3.0.0 cordova-plugin-device@1.1.7 cordova-plugin-dialogs@1.3.4 cordova-plugin-file-transfer@1.7.0...

Next

Cordova iOS 4.5.4
21 Nov 2017 - By Suraj Pindoria

We are happy to announce a minor version of Cordova iOS 4.5.4...