UNPKG

intl-js-sdk-dev

Version:

This is I18n level 2 common library for javascript based clients like g11n-angular-client,g11n-js-client

177 lines (176 loc) 8.7 kB
"use strict"; /* * Copyright 2020 VMware, Inc. * SPDX-License-Identifier: EPL-2.0 * I18n namespace includes DateTimeFormat, NumberFormat, Plural objects. */ Object.defineProperty(exports, "__esModule", { value: true }); const date_formatter_1 = require("../formatters/date.formatter"); const number_formatter_1 = require("../formatters/number.formatter"); const configuration_1 = require("../configuration"); const plurals_func_1 = require("../formatters/plural/plurals.func"); const intl_util_1 = require("./intl.util"); const util_1 = require("../util"); var I18n; (function (I18n) { /* User needs to register locale data by calling the registerLocaleData function before calling other oprations like DateTimeFormat.format() and NumberFormat.format() */ class PatternData { } I18n.PatternData = PatternData; function getCurrencyData(data) { if (util_1.isEmptyObject(data) || util_1.isEmptyObject(data[configuration_1.PatternCategories.CURRENCIES])) { return undefined; } if (data[configuration_1.PatternCategories.CURRENCIES].currencyFormats) { return data[configuration_1.PatternCategories.CURRENCIES]; } const isNumberPatternExist = data[configuration_1.PatternCategories.NUMBER] && !util_1.isEmptyObject(data[configuration_1.PatternCategories.NUMBER]); return isNumberPatternExist ? { currencyFormats: data[configuration_1.PatternCategories.NUMBER].numberFormats.currencyFormats, numberSymbols: data[configuration_1.PatternCategories.NUMBER].numberSymbols, currencySymbols: data[configuration_1.PatternCategories.CURRENCIES], fractions: data['supplemental'].currencies.fractions } : null; } I18n.localeData = {}; function registerLocaleData(locale, pattern) { if (!util_1.isDefined(locale)) { throw new Error(`RegisterLocaleData failed, locale is not provided when calling registerLocaleData function.`); } if (util_1.isEmptyObject(pattern)) { throw new Error(`RegisterLocaleData failed, pattern is not provided when calling registerLocaleData function.`); } I18n.localeData[locale] = pattern; I18n.localeData[locale].currencies = getCurrencyData(pattern); } I18n.registerLocaleData = registerLocaleData; class DateTimeFormat { constructor(locale, options) { this.options = options; this.locale = locale; if (util_1.isEmptyObject(I18n.localeData[locale])) { throw new Error(`Locale data should be registered before creating DateTimeFormat object.`); } if (util_1.isEmptyObject(I18n.localeData[locale].dates)) { throw new Error(`Pattern data for Datetime should be provided before creating DatetimeFormat object.`); } if (!util_1.isDefined(options.minusSign)) { options.minusSign = '-'; } } static getInstance(locale, options) { if (!this._instances.hasOwnProperty(locale)) { this._instances[locale] = new DateTimeFormat(locale, options); this._instanceFormatters[locale] = new date_formatter_1.DateFormatter(I18n.localeData[locale][configuration_1.PatternCategories.DATE]); } else { this._instances[locale].options = options; } return this._instances[locale]; } getStandardTime(date) { return new date_formatter_1.DateFormatter(I18n.localeData[this.locale][configuration_1.PatternCategories.DATE]).getStandardTime(date); } format(date) { return DateTimeFormat._instanceFormatters[this.locale].getformattedString(date, this.options.pattern, this.options.minusSign, this.options.timezone); } } DateTimeFormat._instances = {}; DateTimeFormat._instanceFormatters = {}; I18n.DateTimeFormat = DateTimeFormat; class NumberFormat { constructor(locale, options) { this.options = options; this.locale = locale; if (util_1.isEmptyObject(I18n.localeData[locale])) { throw new Error(`Locale data should be registered before creating NumberFormat object.`); } if (options.numberFormatType === intl_util_1.NumberFormatTypes.DECIMAL || options.numberFormatType === intl_util_1.NumberFormatTypes.PERCENT) { if (util_1.isEmptyObject(I18n.localeData[locale].numbers)) { throw new Error(`Pattern data for Number should be provided before creating NumberFormat object.`); } } if (options.numberFormatType === intl_util_1.NumberFormatTypes.CURRENCIES) { if (util_1.isEmptyObject(I18n.localeData[locale].currencies) || util_1.isEmptyObject(I18n.localeData[locale].numbers)) { throw new Error(`Pattern data for Currency and Number should be provided before creating NumberFormat object.`); } } } static getInstance(locale, options) { if (!this._instances.hasOwnProperty(locale)) { this._instances[locale] = new NumberFormat(locale, options); } else { this._instances[locale].options = options; } return this._instances[locale]; } format(value) { if (this.options.numberFormatType === intl_util_1.NumberFormatTypes.DECIMAL) { return NumberFormat._instanceFormatter.decimal(I18n.localeData[this.locale][configuration_1.PatternCategories.NUMBER], this.locale)(value); } if (this.options.numberFormatType === intl_util_1.NumberFormatTypes.PERCENT) { return NumberFormat._instanceFormatter.percent(I18n.localeData[this.locale][configuration_1.PatternCategories.NUMBER], this.locale)(value); } if (this.options.numberFormatType === intl_util_1.NumberFormatTypes.PLURAL) { return NumberFormat._instanceFormatter.roundNumberForPlural(I18n.localeData[this.locale][configuration_1.PatternCategories.NUMBER], this.locale)(value); } if (this.options.numberFormatType === intl_util_1.NumberFormatTypes.CURRENCIES) { return NumberFormat._instanceFormatter.currencies(I18n.localeData[this.locale][configuration_1.PatternCategories.CURRENCIES], this.locale)(value, this.options.currencyCode); } } } NumberFormat._instances = {}; NumberFormat._instanceFormatter = new number_formatter_1.FormatterFactory(); I18n.NumberFormat = NumberFormat; // End for NumberFormat // Following codes are Plural class class PluralRules { constructor(locale) { this.locale = locale; } static getInstance(locale) { if (!this._instances.hasOwnProperty(locale)) { this._instances[locale] = new PluralRules(locale); } return this._instances[locale]; } resolveLocale(locale) { do { if (plurals_func_1.PLURALFUNCS[locale]) { return locale; } else if (plurals_func_1.PLURALFUNCS[locale.toLocaleLowerCase()]) { return locale.toLocaleLowerCase(); } locale = locale.replace(/(-|)?[^-]*$/, ''); } while (locale); return null; } getFunction() { return plurals_func_1.PLURALFUNCS[this.resolveLocale(this.locale)]; } select(value) { value = intl_util_1.validateNumber(value, 'Plural in message'); // try to round number with default number formatting rules // if data isn't exist, use origin number. // getPattern will fallback to sourceLocale. try { const formatter = PluralRules._instanceFormatter.roundNumberForPlural(I18n.localeData[this.locale][configuration_1.PatternCategories.NUMBER], this.locale); value = Number(formatter(value)); } catch (error) { } value = Math.abs(value); const pluraFunction = this.getFunction(); const type = pluraFunction ? pluraFunction(value) : undefined; return type; } } PluralRules._instances = {}; PluralRules._instanceFormatter = new number_formatter_1.FormatterFactory(); I18n.PluralRules = PluralRules; // End for Plural })(I18n = exports.I18n || (exports.I18n = {}));