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:

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:

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:

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: