cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sgrebnov <...@git.apache.org>
Subject [GitHub] cordova-plugin-globalization pull request: CB-7972 Add cordova-plu...
Date Sun, 07 Dec 2014 12:13:43 GMT
Github user sgrebnov commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-globalization/pull/31#discussion_r21424012
  
    --- Diff: src/windows/GlobalizationProxy.js ---
    @@ -0,0 +1,294 @@
    +/*  
    +	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
    +	
    +	http://www.apache.org/licenses/LICENSE-2.0
    +	
    +	Unless required by applicable law or agreed to in writing, software
    +	distributed under the License is distributed on an "AS IS" BASIS,
    +	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +	See the License for the specific language governing permissions and
    +	limitations under the License.
    +*/
    +
    +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);
    +        }
    +    },
    +
    +    getLocaleName: function (win, fail) {
    +        // Corresponds to a user-selected regional format
    +        win({ value: locale });
    +    },
    +
    +    dateToString: function (win, fail, args) {
    +        tryDoAction(GlobalizationProxy.GlobalizationProxy.dateToString,
    +            JSON.stringify({
    +                date: args[0].date,
    +                options: args[0].options || { formatLength: "short", selector: "date
and time" }
    +            }), win, fail);
    +    },
    +
    +    stringToDate: function (win, fail, args) {
    +        tryDoAction(GlobalizationProxy.GlobalizationProxy.stringToDate, 
    +            JSON.stringify({
    +                dateString: args[0].dateString,
    +                options: args[0].options || { formatLength: "short", selector: "date
and time" }
    +            }), win, fail);
    +    },
    +
    +    getDateNames: function (win, fail, args) {
    +        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);
    --- End diff --
    
    why 2014 is hardcoded?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


Mime
View raw message