nativescript-intl.android.js 6.65 KB
Newer Older
patcharaporn's avatar
patcharaporn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var nativescript_intl_common_1 = require("./nativescript-intl-common");
var localesCache = new Map();
function getNativeLocale(locale) {
    if (localesCache.has(locale)) {
        return localesCache.get(locale);
    }
    var result;
    if (locale) {
        locale = locale.replace(/_/g, "-");
        var firstHypenIndex = locale.indexOf("-");
        var lang = "";
        var country = "";
        if (firstHypenIndex > -1) {
            lang = locale.substr(0, firstHypenIndex);
            var nextHypenIndex = locale.substr(firstHypenIndex + 1).indexOf("-");
            country = locale.substr(firstHypenIndex + 1, (nextHypenIndex > -1) ? nextHypenIndex : undefined);
        }
        else {
            lang = locale;
        }
        if (country !== "") {
            result = new java.util.Locale(lang, country);
        }
        else {
            result = new java.util.Locale(lang);
        }
    }
    else {
        result = new java.util.Locale("en", "US");
    }
    localesCache.set(locale, result);
    return result;
}
var DateTimeFormat = (function (_super) {
    __extends(DateTimeFormat, _super);
    function DateTimeFormat() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    DateTimeFormat.prototype.getNativePattern = function (patternDefinition, locale) {
        var result = "";
        var flag = 0;
        var nativeLocale;
        if (locale) {
            nativeLocale = getNativeLocale(locale);
            flag++;
        }
        if (patternDefinition.date) {
            flag = flag + 2;
        }
        if (patternDefinition.time) {
            flag = flag + 4;
        }
        var dateFormat;
        switch (flag) {
            case 0:
                dateFormat = java.text.DateFormat.getDateTimeInstance();
                break;
            case 1:
                dateFormat = java.text.DateFormat.getDateTimeInstance(0, 0, nativeLocale);
                break;
            case 2:
                dateFormat = java.text.DateFormat.getDateInstance(patternDefinition.date === nativescript_intl_common_1.FULL ? 0 : 3);
                break;
            case 3:
                dateFormat =
                    java.text.DateFormat.getDateInstance(patternDefinition.date === nativescript_intl_common_1.FULL ? 0 : 3, nativeLocale);
                break;
            case 4:
                dateFormat = java.text.DateFormat.getTimeInstance(1);
                break;
            case 5:
                dateFormat = java.text.DateFormat.getTimeInstance(1, nativeLocale);
                break;
            case 6:
                dateFormat = java.text.DateFormat.getDateTimeInstance(patternDefinition.date === nativescript_intl_common_1.FULL ? 0 : 3, 1);
                break;
            case 7:
                dateFormat =
                    java.text.DateFormat.getDateTimeInstance(patternDefinition.date === nativescript_intl_common_1.FULL ? 0 : 3, 1, nativeLocale);
                break;
            default:
                break;
        }
        result = dateFormat.toPattern();
        return result;
    };
    DateTimeFormat.prototype.formatNative = function (pattern, locale, date) {
        var sdf = locale ?
            new java.text.SimpleDateFormat(pattern, getNativeLocale(locale)) :
            new java.text.SimpleDateFormat(pattern);
        return sdf.format(date ? new java.util.Date(date.valueOf()) : new java.util.Date()).toString();
    };
    return DateTimeFormat;
}(nativescript_intl_common_1.DateTimeFormat));
exports.DateTimeFormat = DateTimeFormat;
var NumberFormat = (function (_super) {
    __extends(NumberFormat, _super);
    function NumberFormat() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    NumberFormat.prototype.formatNative = function (value, locale, options, pattern) {
        var numberFormat;
        if (pattern) {
            numberFormat = new java.text.DecimalFormat(pattern);
        }
        else {
            if (options) {
                switch (options.style.toLowerCase()) {
                    case "decimal":
                        numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
                        break;
                    case "percent":
                        numberFormat = java.text.NumberFormat.getPercentInstance(getNativeLocale(locale));
                        break;
                    case "currency":
                        numberFormat = java.text.NumberFormat.getCurrencyInstance(getNativeLocale(locale));
                        if (options.currency !== void 0) {
                            numberFormat.setCurrency(java.util.Currency.getInstance(options.currency));
                        }
                        break;
                    default:
                        numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
                        break;
                }
            }
            else {
                numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
            }
        }
        if (options && options.minimumIntegerDigits !== void 0) {
            numberFormat.setMinimumIntegerDigits(options.minimumIntegerDigits);
        }
        if (options && options.minimumFractionDigits !== void 0) {
            numberFormat.setMinimumFractionDigits(options.minimumFractionDigits);
        }
        if (options && options.maximumFractionDigits !== void 0) {
            numberFormat.setMaximumFractionDigits(options.maximumFractionDigits);
        }
        if (options && options.useGrouping !== void 0) {
            numberFormat.setGroupingUsed(options.useGrouping);
        }
        var decimalFormatSymbols = locale ?
            new java.text.DecimalFormatSymbols(getNativeLocale(locale)) :
            new java.text.DecimalFormatSymbols();
        numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
        if (options && (options.style.toLowerCase() === "currency" && options.currencyDisplay === "code")) {
            if (!pattern) {
                var currrentPattern = numberFormat.toPattern();
                currrentPattern = currrentPattern.replace("¤", "¤¤");
                numberFormat = new java.text.DecimalFormat(currrentPattern);
                numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
            }
            if (options.currency !== void 0) {
                decimalFormatSymbols.setCurrency(java.util.Currency.getInstance(options.currency));
            }
        }
        return numberFormat.format(value);
    };
    return NumberFormat;
}(nativescript_intl_common_1.NumberFormat));
exports.NumberFormat = NumberFormat;