UNPKG

g11n-js-common

Version:

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

97 lines (96 loc) 4.08 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"); 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() */ // export interface IPatternData { // dates: any; // numbers: any; // plurals: any; // currencies: any; // } class PatternData { } I18n.PatternData = PatternData; I18n.localeData = {}; function registerLocaleData(locale, pattern) { I18n.localeData[locale] = pattern; } I18n.registerLocaleData = registerLocaleData; class DateTimeFormatter { getStandardTime(date) { return new date_formatter_1.DateFormatter().getStandardTime(date); } } I18n.DateTimeFormatter = DateTimeFormatter; class DateTimeFormat { constructor(locale, options) { this.options = options; this.locale = locale; } format(date) { const datetimeformatter = new date_formatter_1.DateFormatter(); return datetimeformatter.getformattedString(date, this.options.pattern, I18n.localeData[this.locale][configuration_1.PatternCategories.DATE], this.options.minusSign, this.options.timezone); } } I18n.DateTimeFormat = DateTimeFormat; class NumberFormat { constructor(locale, options) { this.options = options; this.locale = locale; } format(value) { const numberformatter = new number_formatter_1.FormatterFactory(); if (this.options.numberFormatType === intl_util_1.NumberFormatTypes.DECIMAL) { return numberformatter.decimal(I18n.localeData[this.locale][configuration_1.PatternCategories.NUMBER], this.locale)(value); } if (this.options.numberFormatType === intl_util_1.NumberFormatTypes.PERCENT) { return numberformatter.percent(I18n.localeData[this.locale][configuration_1.PatternCategories.NUMBER], this.locale)(value); } if (this.options.numberFormatType === intl_util_1.NumberFormatTypes.PLURAL) { return numberformatter.roundNumberForPlural(I18n.localeData[this.locale][configuration_1.PatternCategories.PLURAL], this.locale)(value); } if (this.options.numberFormatType === intl_util_1.NumberFormatTypes.CURRENCIES) { return numberformatter.currencies(I18n.localeData[this.locale][configuration_1.PatternCategories.CURRENCIES], this.locale)(value, this.options.currencyCode); } } } I18n.NumberFormat = NumberFormat; // End for NumberFormat // Following codes are Plural class class Plural { constructor() { } 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; } getFunc(locale) { this._locale = this.resolveLocale(locale); return plurals_func_1.PLURALFUNCS[this._locale]; } } I18n.Plural = Plural; // End for Plural })(I18n = exports.I18n || (exports.I18n = {}));