You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2017/11/21 22:30:21 UTC

[GitHub] maverickmishra closed pull request #768: Added blog post about Migration from Globalization Plugin

maverickmishra closed pull request #768: Added blog post about Migration from Globalization Plugin
URL: https://github.com/apache/cordova-docs/pull/768
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/www/_posts/2017-11-20-migrate-from-cordova-globalization-plugin.md b/www/_posts/2017-11-20-migrate-from-cordova-globalization-plugin.md
new file mode 100644
index 000000000..7d8551282
--- /dev/null
+++ b/www/_posts/2017-11-20-migrate-from-cordova-globalization-plugin.md
@@ -0,0 +1,82 @@
+---
+layout: post
+author:
+    name: Vishal Mishra
+    url: https://twitter.com/tweetsbymishra
+title:  "Migrating from the Globalization Plugin"
+categories: news
+tags: cordova globalization
+---
+
+## 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](https://www.ecma-international.org/ecma-402/1.0/) 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:
+
+```js
+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:
+
+```js
+navigator.globalization.getLocaleName(function (locale) {          
+    console.log('locale: ' + locale.value + '\n');
+}, function () {
+    console.log('Error getting locale\n'); 
+});
+```
+
+The [ECMA Internationalization API](https://www.ecma-international.org/ecma-402/1.0/) 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:
+
+```js
+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:
+
+```js
+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:
+
+```js
+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:
+
+```js
+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:
+
+```js
+var date = new Date();
+console.log(new Intl.DateTimeFormat().format(date));
+```
+
+[Here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) is a good resource to find out more about various methods in the [ECMA Internationalization API](https://www.ecma-international.org/ecma-402/1.0/).
+
+Your feedback is graciously accepted and appreciated!


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org