cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [3/4] cordova-plugin-globalization git commit: CB-7972 Add cordova-plugin-globalization support for Windows platform
Date Tue, 13 Jan 2015 20:03:44 GMT
CB-7972 Add cordova-plugin-globalization support for Windows platform

Reworked the plugin to use WinJS libraries where possible
Updated the docs
Fixed `negative` property in .NET' getNumberPattern
Fixed `stringToDate` method - months were shifted +1
Fixed getPreferredLanguage test to be aligned with BCP-47


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/commit/9d60ee8e
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/tree/9d60ee8e
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/diff/9d60ee8e

Branch: refs/heads/master
Commit: 9d60ee8ec91216deafb44b3678a11fbb7c2ec7b7
Parents: 46e575d
Author: daserge <daserge@yandex.ru>
Authored: Fri Dec 5 23:04:52 2014 +0300
Committer: daserge <daserge@yandex.ru>
Committed: Sat Dec 6 12:29:04 2014 +0300

----------------------------------------------------------------------
 doc/index.md                                    |  28 ++-
 src/windows/GlobalizationProxy.js               | 230 ++++++++++++++++---
 src/windows/GlobalizationProxy.winmd            | Bin 20992 -> 20992 bytes
 .../Globalization/GlobalizationImpl.cs          |   8 +-
 tests/tests.js                                  |  10 +-
 5 files changed, 235 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/9d60ee8e/doc/index.md
----------------------------------------------------------------------
diff --git a/doc/index.md b/doc/index.md
index 67279ff..979b2af 100644
--- a/doc/index.md
+++ b/doc/index.md
@@ -168,7 +168,7 @@ by a hyphen.
 ### Windows Quirks
 
 - Locale setting can be changed in Control Panel -> Clock, Language and Region 
--> Region -> Administrative -> Change system locale on Windows 8.1/8.0, 
+-> Region -> Formats -> Format, 
 and in Settings -> Region -> Regional Format on Windows Phone 8.1.
 
 
@@ -286,6 +286,7 @@ error's expected code is `GlobalizationError.FORMATTING_ERROR`.
 - Android
 - BlackBerry 10
 - iOS
+- Windows
 
 ### Example
 
@@ -315,6 +316,10 @@ Expected result:
     decimal: .
     grouping: ,
 
+### Windows Quirks
+
+- Only 'code' and 'fraction' properties are supported
+
 
 ## navigator.globalization.getDateNames
 
@@ -380,7 +385,7 @@ a series of twelve popup dialogs, one per month, with text similar to
 
 ### Windows Quirks
 
-- The array of months contains 13 elements.
+- The array of months contains 12 elements.
 - The returned array may be not completely aligned with ICU depending on a user locale.
 
 ## navigator.globalization.getDatePattern
@@ -590,8 +595,6 @@ Results:
 
 - The `pattern` property is not supported, and returns an empty string.
 
-- The `fraction` property is not supported, and returns zero.
-
 
 ## navigator.globalization.isDayLightSavingsTime
 
@@ -680,6 +683,13 @@ dialog with text similar to `number: 3.142`:
         {type:'decimal'}
     );
 
+### Windows Quirks
+
+- Windows 8.0 does not support number rounding, therefore values will not be rounded automatically.
+
+- On Windows 8.1 and Windows Phone 8.1 fractional part is being truncated instead of rounded
in case of `percent` number type therefore fractional digits count is set to 0.
+
+- `percent` numbers are not grouped as they can't be parsed in stringToNumber if grouped.
 
 ## navigator.globalization.stringToDate
 
@@ -813,6 +823,16 @@ popup dialog with text similar to `number: 1234.56`:
         {type:'decimal'}
     );
 
+### Windows Phone 8 Quirks	
+
+- In case of `percent` type the returned value is not divided by 100.
+
+### Windows Quirks	
+	
+- The string must strictly conform to the locale format. For example, percent symbol should
be 
+separated by space for 'en-US' locale if the type parameter is 'percent'.
+
+- `percent` numbers must not be grouped to be parsed correctly.
 
 ## GlobalizationError
 

http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/9d60ee8e/src/windows/GlobalizationProxy.js
----------------------------------------------------------------------
diff --git a/src/windows/GlobalizationProxy.js b/src/windows/GlobalizationProxy.js
index 1edb233..9297d79 100644
--- a/src/windows/GlobalizationProxy.js
+++ b/src/windows/GlobalizationProxy.js
@@ -1,4 +1,4 @@
-/*  
+/*  
 	Licensed under the Apache License, Version 2.0 (the "License");
 	you may not use this file except in compliance with the License.
 	You may obtain a copy of the License at
@@ -13,11 +13,98 @@
 */
 
 var GlobalizationError = require('./GlobalizationError');
+var locale = navigator.userLanguage;
+
+var decimalFormatter;
+var currencyFormatter;
+var percentFormatter;
+
+function createRoundingAlgorithm() {
+    // This is undefined in case of Windows 8.0
+    if (Windows.Globalization.NumberFormatting.IncrementNumberRounder) {
+        var rounder = new Windows.Globalization.NumberFormatting.IncrementNumberRounder();
+        rounder.roundingAlgorithm = Windows.Globalization.NumberFormatting.RoundingAlgorithm.roundHalfUp;
+        rounder.increment = 0.01;
+
+        return rounder;
+    }
+
+    return null;
+}
+
+function createDecimalFormatter(regionName) {
+    decimalFormatter = new Windows.Globalization.NumberFormatting.DecimalFormatter([locale],
regionName);
+
+    decimalFormatter.numberRounder = createRoundingAlgorithm();
+    decimalFormatter.isGrouped = false;
+
+    return decimalFormatter;
+}
+
+function getDecimalFormatter(regionName) {
+    return decimalFormatter || createDecimalFormatter(regionName);
+}
+
+function createCurrencyFormatter(regionName) {
+    var regionObj = new Windows.Globalization.GeographicRegion(regionName);
+    var currency = regionObj.currenciesInUse[0];
+
+    currencyFormatter = new Windows.Globalization.NumberFormatting.CurrencyFormatter(currency,
[locale], regionName);
+
+    currencyFormatter.numberRounder = createRoundingAlgorithm();
+    currencyFormatter.isGrouped = true;
+
+    return currencyFormatter;
+}
+
+function getCurrencyFormatter(regionName) {
+    return currencyFormatter || createCurrencyFormatter(regionName);
+}
+
+function createPercentFormatter(regionName) {
+    percentFormatter = new Windows.Globalization.NumberFormatting.PercentFormatter([locale],
regionName);
+
+    percentFormatter.numberRounder = createRoundingAlgorithm();
+    percentFormatter.fractionDigits = 0;
+    percentFormatter.isGrouped = false;
+
+    return percentFormatter;
+}
+
+function getPercentFormatter(regionName) {
+    return percentFormatter || createPercentFormatter(regionName);
+}
+
+function getNumberFormatter(options) {
+    options = options || { type: 'decimal' };
+    options.type = options.type || 'decimal';
+
+    var tags = locale.split('-');
+    var regionName = tags[tags.length - 1];
+
+    switch (options.type) {
+        case 'decimal':
+        {
+            return getDecimalFormatter(regionName);
+        }
+        case 'currency':
+        {
+            return getCurrencyFormatter(regionName);
+        }
+        case 'percent':
+        {
+            return getPercentFormatter(regionName);
+        }
+        default:
+            throw "The options.type can be 'decimal', 'percent', or 'currency' only";
+    };
+}
 
 module.exports = {
     getPreferredLanguage: function (win, fail) {
         try {
             var language = Windows.System.UserProfile.GlobalizationPreferences.languages[0];
+
             win({ value: language });
         } catch (e) {
             fail(e);
@@ -25,16 +112,15 @@ module.exports = {
     },
 
     getLocaleName: function (win, fail) {
-        tryDoAction(GlobalizationProxy.GlobalizationProxy.getLocaleName, "", win, fail);
+        // Corresponds to a user-selected regional format
+        win({ value: locale });
     },
 
     dateToString: function (win, fail, args) {
-        args[0].options = args[0].options || { formatLength: "full", selector: "date and
time" };        
-
-        tryDoAction(GlobalizationProxy.GlobalizationProxy.dateToString, 
+        tryDoAction(GlobalizationProxy.GlobalizationProxy.dateToString,
             JSON.stringify({
                 date: args[0].date,
-                options: args[0].options
+                options: args[0].options || { formatLength: "short", selector: "date and
time" }
             }), win, fail);
     },
 
@@ -42,15 +128,58 @@ module.exports = {
         tryDoAction(GlobalizationProxy.GlobalizationProxy.stringToDate, 
             JSON.stringify({
                 dateString: args[0].dateString,
-                options: args[0].options
+                options: args[0].options || { formatLength: "short", selector: "date and
time" }
             }), win, fail);
     },
 
     getDateNames: function (win, fail, args) {
-        tryDoAction(GlobalizationProxy.GlobalizationProxy.getDateNames, 
-            JSON.stringify({
-                options: args[0].options
-            }), win, fail);
+        try {
+            var options = args[0].options || { type: 'wide', item: 'months' };
+            var type = options.type || 'wide';
+            var item = options.item || 'months';
+
+            var monthFormat = Windows.Globalization.DateTimeFormatting.MonthFormat.none;
+            var dayOfWeekFormat = Windows.Globalization.DateTimeFormatting.DayOfWeekFormat.none;
+
+            if (item === 'months' && type === 'wide') {
+                monthFormat = Windows.Globalization.DateTimeFormatting.MonthFormat.full;
+            } else if (item === 'months' && type === 'narrow') {
+                monthFormat = Windows.Globalization.DateTimeFormatting.MonthFormat.abbreviated;
+            } else if (item === 'days' && type === 'wide') {
+                dayOfWeekFormat = Windows.Globalization.DateTimeFormatting.DayOfWeekFormat.full;
+            } else if (item === 'days' && type === 'narrow') {
+                dayOfWeekFormat = Windows.Globalization.DateTimeFormatting.DayOfWeekFormat.abbreviated;
+            } else {
+                throw "Incorrect item type";
+            }
+
+            var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter(
+                Windows.Globalization.DateTimeFormatting.YearFormat.none,
+                monthFormat, 
+                Windows.Globalization.DateTimeFormatting.DayFormat.none, 
+                dayOfWeekFormat, 
+                Windows.Globalization.DateTimeFormatting.HourFormat.none, 
+                Windows.Globalization.DateTimeFormatting.MinuteFormat.none, 
+                Windows.Globalization.DateTimeFormatting.SecondFormat.none, 
+                [locale]);
+
+            var result = [];
+            if (item === 'months') {
+                for (var i = 0; i < 12; i++) {
+                    var date = new Date(2014, i, 20, 0, 0, 0, 0);
+                    result[i] = formatter.format(date);
+                }
+            } else {
+                for (i = 0; i < 7; i++) {
+                    date = new Date(2014, 5, i + 1, 0, 0, 0, 0);
+                    result[i] = formatter.format(date);
+                }
+            }
+
+            win({ value: result });
+        } catch (e) {
+            fail(new GlobalizationError(0, e));
+        }
     },
 
     isDayLightSavingsTime: function (win, fail, args) {
@@ -61,46 +190,88 @@ module.exports = {
     },
 
     getFirstDayOfWeek: function (win, fail) {
-        tryDoAction(GlobalizationProxy.GlobalizationProxy.getFirstDayOfWeek, "", win, fail);
+        win({ value: Windows.System.UserProfile.GlobalizationPreferences.weekStartsOn + 1
});
     },
 
     numberToString: function (win, fail, args) {
-        tryDoAction(GlobalizationProxy.GlobalizationProxy.numberToString, 
-            JSON.stringify({
-                number: args[0].number,
-                options: args[0].options
-            }), win, fail);
+        try {
+            var formatter = getNumberFormatter(args[0].options);
+            var formattedNumber = formatter.format(args[0].number);
+            if (!formattedNumber) {
+                 throw 'Unknown error';
+            }
+
+            win({ value: formattedNumber });
+        } catch (e) {
+            fail(new GlobalizationError(GlobalizationError.FORMATTING_ERROR, e));
+        }
     },
 
     stringToNumber: function (win, fail, args) {
-        tryDoAction(GlobalizationProxy.GlobalizationProxy.stringToNumber, 
-            JSON.stringify({
-                numberString: args[0].numberString,
-                options: args[0].options
-            }), win, fail);
+        try {
+            var formatter = getNumberFormatter(args[0].options);
+            var number = formatter.parseDouble(args[0].numberString);
+            if (!number) {
+                 throw 'Input string was in incorrect format';
+            }
+
+            win({ value: number });
+        } catch (e) {
+            fail(new GlobalizationError(GlobalizationError.PARSING_ERROR, e));
+        }
     },
 
     getDatePattern: function (win, fail, args) {
-        tryDoAction(GlobalizationProxy.GlobalizationProxy.getDatePattern, 
+        tryDoAction(GlobalizationProxy.GlobalizationProxy.getDatePattern,
             JSON.stringify({
                 options: args[0].options
             }), win, fail);
     },
 
     getNumberPattern: function (win, fail, args) {
-        tryDoAction(GlobalizationProxy.GlobalizationProxy.getNumberPattern, 
-            JSON.stringify({
+        try {
+            var result = GlobalizationProxy.GlobalizationProxy.getNumberPattern(JSON.stringify({
                 options: args[0].options
-            }), win, fail);
+            }));
+
+            var obj = JSON.parse(result);
+            checkForGlobalizationError(obj);
+
+            var formatter = getNumberFormatter(args[0].options);
+            obj.fraction = formatter.fractionDigits;
+
+            win(obj);
+        } catch (e) {
+            fail(e);
+        }
     },
 
     getCurrencyPattern: function (win, fail, args) {
-        fail("Not implemented");
+        try {
+            var tags = locale.split('-');
+            var regionName = tags[tags.length - 1];
+            var currency = args[0].currencyCode;
+            var formatter = new Windows.Globalization.NumberFormatting.CurrencyFormatter(
+                currency, [locale], regionName);
+
+            win({
+                fraction: formatter.fractionDigits,
+                code: formatter.currency,
+
+                //unsupported
+                decimal: '',
+                grouping: '',
+                pattern: '',
+                rounding: 0,
+            });
+        } catch (e) {
+            fail(new GlobalizationError(GlobalizationError.PATTERN_ERROR, e));
+        }
     }
 };
 
 function tryDoAction(action, args, win, fail) {
-    try {        
+    try {
         var result = action(args);
         var obj = JSON.parse(result);
         checkForGlobalizationError(obj);
@@ -117,8 +288,7 @@ function checkForGlobalizationError(obj) {
 }
 
 (function init() {
-    // Actually navigator.systemLanguage returns a locale
-    GlobalizationProxy.GlobalizationProxy.setLocale(navigator.systemLanguage);
+    GlobalizationProxy.GlobalizationProxy.setLocale(locale);
 })();
 
 require("cordova/exec/proxy").add("Globalization", module.exports);
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/9d60ee8e/src/windows/GlobalizationProxy.winmd
----------------------------------------------------------------------
diff --git a/src/windows/GlobalizationProxy.winmd b/src/windows/GlobalizationProxy.winmd
index 1248be0..bf61069 100644
Binary files a/src/windows/GlobalizationProxy.winmd and b/src/windows/GlobalizationProxy.winmd
differ

http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/9d60ee8e/src/windows/GlobalizationProxy/GlobalizationProxy/Globalization/GlobalizationImpl.cs
----------------------------------------------------------------------
diff --git a/src/windows/GlobalizationProxy/GlobalizationProxy/Globalization/GlobalizationImpl.cs
b/src/windows/GlobalizationProxy/GlobalizationProxy/Globalization/GlobalizationImpl.cs
index 24d181f..7e2b772 100644
--- a/src/windows/GlobalizationProxy/GlobalizationProxy/Globalization/GlobalizationImpl.cs
+++ b/src/windows/GlobalizationProxy/GlobalizationProxy/Globalization/GlobalizationImpl.cs
@@ -266,7 +266,7 @@ namespace GlobalizationProxy.Globalization
             }
 
             DateTime date = DateTime.ParseExact(globalOptions.DateString, format, CultureInfo.CurrentCulture);
-            return new DateFormat(date.Year, date.Month, date.Day, date.Hour, date.Minute,
date.Second, date.Millisecond);
+            return new DateFormat(date.Year, date.Month - 1, date.Day, date.Hour, date.Minute,
date.Second, date.Millisecond);
         }
 
         /// <summary>
@@ -519,19 +519,19 @@ namespace GlobalizationProxy.Globalization
                 case GlobalizationOptions.Percent:
                     {
                         symbol = formatInfo.PercentSymbol;
-                        pattern = new NumberPattern("", symbol, 0, formatInfo.PercentPositivePattern.ToString(),
formatInfo.PercentNegativePattern.ToString(), 0, formatInfo.PercentDecimalSeparator, formatInfo.PercentGroupSeparator);
+                        pattern = new NumberPattern("", symbol, 0, formatInfo.PercentPositivePattern.ToString(),
formatInfo.NegativeSign, 0, formatInfo.PercentDecimalSeparator, formatInfo.PercentGroupSeparator);
                         break;
                     }
                 case GlobalizationOptions.Currency:
                     {
                         symbol = formatInfo.CurrencySymbol;
-                        pattern = new NumberPattern("", symbol, 0, formatInfo.CurrencyPositivePattern.ToString(),
formatInfo.CurrencyNegativePattern.ToString(), 0, formatInfo.CurrencyDecimalSeparator, formatInfo.CurrencyGroupSeparator);
+                        pattern = new NumberPattern("", symbol, 0, formatInfo.CurrencyPositivePattern.ToString(),
formatInfo.NegativeSign, 0, formatInfo.CurrencyDecimalSeparator, formatInfo.CurrencyGroupSeparator);
                         break;
                     }
                 default:
                     {
                         symbol = formatInfo.NumberDecimalSeparator;
-                        pattern = new NumberPattern("", symbol, 0, "", formatInfo.NumberNegativePattern.ToString(),
0, formatInfo.NumberDecimalSeparator, formatInfo.NumberGroupSeparator);
+                        pattern = new NumberPattern("", symbol, 0, "", formatInfo.NegativeSign,
0, formatInfo.NumberDecimalSeparator, formatInfo.NumberGroupSeparator);
                         break;
                     }
             }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/9d60ee8e/tests/tests.js
----------------------------------------------------------------------
diff --git a/tests/tests.js b/tests/tests.js
index 27b5dc0..3362363 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -50,12 +50,16 @@ exports.defineAutoTests = function () {
                 },
                 fail.bind(null, done));
             });
-            it("globalization.spec.4 getPreferredLanguage return string should have a hyphen",
function (done) {
+            it("globalization.spec.4 getPreferredLanguage return string should contain one
or more language subtags separated by hyphen", function (done) {
                 navigator.globalization.getPreferredLanguage(function (a) {
                     checkPreferredLanguage(a);
                     expect(a.value.indexOf('_')).toBe(-1);
-                    expect(a.value.indexOf('-')).toBeGreaterThan(0);
-                    done()
+
+                    // A language tag is composed from a sequence of one or more "subtags",
separated by hyphen.
+                    // https://tools.ietf.org/html/bcp47#section-2.1
+                    expect(a.value.split('-').length).toBeGreaterThan(0); 
+
+                    done();
                 }, fail.bind(null, done));
             });
         });


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


Mime
View raw message