← Plugins / Globalization
This official Apache Cordova plugin reports locale- and language-specific information from the device and formats dates, numbers and currencies according to the user's settings. It's the building block for apps that need to feel native in every region.
cordova plugin add cordova-plugin-globalization
All methods live on navigator.globalization and are asynchronous — each takes a success and an error callback. Call them after deviceready:
| Method | Returns |
|---|---|
getPreferredLanguage(success, error) | The user's current language, e.g. { value: "English" }. |
getLocaleName(success, error) | The device locale, e.g. { value: "en-US" }. |
dateToString(date, success, error, options) | A locale-formatted date string. |
stringToDate(string, success, error, options) | A parsed date from a locale string. |
getDatePattern(success, error, options) | The locale's date pattern and time zone. |
getFirstDayOfWeek(success, error) | The first day of the week for the locale. |
numberToString(number, success, error, options) | A locale-formatted number, percent or currency string. |
getCurrencyPattern(currencyCode, success, error) | The formatting pattern for a currency. |
Read the device locale and format today's date for it:
document.addEventListener("deviceready", onReady, false);
function onReady() {
navigator.globalization.getLocaleName(
function (locale) {
console.log("Locale: " + locale.value);
},
function () {
console.log("Could not read locale");
}
);
navigator.globalization.dateToString(
new Date(),
function (date) {
console.log("Today: " + date.value);
},
function () {},
{ formatLength: "medium", selector: "date" }
);
}
getPreferredLanguage returns the language; getLocaleName returns the full locale code.Intl API can be a lighter alternative.