angular-l10n
Version:
An Angular library to translate messages, dates and numbers
686 lines • 23.7 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
import * as tslib_1 from "tslib";
import { Injectable, Inject } from '@angular/core';
import { Subject } from 'rxjs';
import { IntlAPI } from './intl-api';
import { LocaleStorage } from './locale-storage';
import { L10N_CONFIG } from "../models/l10n-config";
import { DefaultLocale } from '../models/default-locale';
import { IntlFormatter } from '../models/intl-formatter';
import { ISOCode, NumberFormatStyle } from '../models/types';
/**
* @record
*/
export function ILocaleService() { }
if (false) {
/** @type {?} */
ILocaleService.prototype.languageCodeChanged;
/** @type {?} */
ILocaleService.prototype.defaultLocaleChanged;
/** @type {?} */
ILocaleService.prototype.currencyCodeChanged;
/** @type {?} */
ILocaleService.prototype.timezoneChanged;
/**
* @return {?}
*/
ILocaleService.prototype.getConfiguration = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.init = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getBrowserLanguage = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getAvailableLanguages = function () { };
/**
* @param {?=} languageCode
* @return {?}
*/
ILocaleService.prototype.getLanguageDirection = function (languageCode) { };
/**
* @return {?}
*/
ILocaleService.prototype.getCurrentLanguage = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getCurrentCountry = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getCurrentScript = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getCurrentLocale = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getCurrentNumberingSystem = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getCurrentCalendar = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getDefaultLocale = function () { };
/**
* @return {?}
*/
ILocaleService.prototype.getCurrentCurrency = function () { };
/**
* @param {?=} currencyDisplay
* @param {?=} defaultLocale
* @param {?=} currency
* @return {?}
*/
ILocaleService.prototype.getCurrencySymbol = function (currencyDisplay, defaultLocale, currency) { };
/**
* @return {?}
*/
ILocaleService.prototype.getCurrentTimezone = function () { };
/**
* @param {?} languageCode
* @return {?}
*/
ILocaleService.prototype.setCurrentLanguage = function (languageCode) { };
/**
* @param {?} languageCode
* @param {?=} countryCode
* @param {?=} scriptCode
* @param {?=} numberingSystem
* @param {?=} calendar
* @return {?}
*/
ILocaleService.prototype.setDefaultLocale = function (languageCode, countryCode, scriptCode, numberingSystem, calendar) { };
/**
* @param {?} currencyCode
* @return {?}
*/
ILocaleService.prototype.setCurrentCurrency = function (currencyCode) { };
/**
* @param {?} zoneName
* @return {?}
*/
ILocaleService.prototype.setCurrentTimezone = function (zoneName) { };
/**
* @param {?} value
* @param {?=} format
* @param {?=} defaultLocale
* @param {?=} timezone
* @return {?}
*/
ILocaleService.prototype.formatDate = function (value, format, defaultLocale, timezone) { };
/**
* @param {?} value
* @param {?} unit
* @param {?=} format
* @param {?=} defaultLocale
* @return {?}
*/
ILocaleService.prototype.formatRelativeTime = function (value, unit, format, defaultLocale) { };
/**
* @param {?} value
* @param {?=} digits
* @param {?=} defaultLocale
* @return {?}
*/
ILocaleService.prototype.formatDecimal = function (value, digits, defaultLocale) { };
/**
* @param {?} value
* @param {?=} digits
* @param {?=} defaultLocale
* @return {?}
*/
ILocaleService.prototype.formatPercent = function (value, digits, defaultLocale) { };
/**
* @param {?} value
* @param {?=} digits
* @param {?=} currencyDisplay
* @param {?=} defaultLocale
* @param {?=} currency
* @return {?}
*/
ILocaleService.prototype.formatCurrency = function (value, digits, currencyDisplay, defaultLocale, currency) { };
/**
* @param {?} codes
* @return {?}
*/
ILocaleService.prototype.composeLocale = function (codes) { };
/**
* @return {?}
*/
ILocaleService.prototype.rollback = function () { };
}
/**
* Manages language, default locale, currency & timezone.
*/
export class LocaleService {
/**
* @param {?} configuration
* @param {?} storage
*/
constructor(configuration, storage) {
this.configuration = configuration;
this.storage = storage;
/**
* Fired when the language changes. Returns the language code.
*/
this.languageCodeChanged = new Subject();
/**
* Fired when the default locale changes. Returns the default locale.
*/
this.defaultLocaleChanged = new Subject();
/**
* Fired when the currency changes. Returns the currency code.
*/
this.currencyCodeChanged = new Subject();
/**
* Fired when the timezone changes. Returns the timezone.
*/
this.timezoneChanged = new Subject();
this.defaultLocale = new DefaultLocale();
}
/**
* @return {?}
*/
getConfiguration() {
return this.configuration.locale;
}
/**
* @return {?}
*/
init() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
yield this.initLanguage();
yield this.initDefaultLocale();
yield this.initCurrency();
yield this.initTimezone();
});
}
/**
* @return {?}
*/
getBrowserLanguage() {
/** @type {?} */
let browserLanguage = null;
if (typeof navigator !== "undefined" && navigator.language) {
browserLanguage = navigator.language;
}
if (browserLanguage != null) {
browserLanguage = browserLanguage.split("-")[0];
}
return browserLanguage;
}
/**
* @return {?}
*/
getAvailableLanguages() {
/** @type {?} */
let languages = [];
if (this.configuration.locale.languages) {
languages = this.configuration.locale.languages.map((language) => language.code);
}
return languages;
}
/**
* @param {?=} languageCode
* @return {?}
*/
getLanguageDirection(languageCode = this.defaultLocale.languageCode) {
/** @type {?} */
const matchedLanguage = this.matchLanguage(languageCode);
return matchedLanguage ? matchedLanguage.dir : "";
}
/**
* @return {?}
*/
getCurrentLanguage() {
return this.defaultLocale.languageCode;
}
/**
* @return {?}
*/
getCurrentCountry() {
return this.defaultLocale.countryCode || "";
}
/**
* @return {?}
*/
getCurrentScript() {
return this.defaultLocale.scriptCode || "";
}
/**
* Returns the well formatted locale as {languageCode}[-scriptCode][-countryCode]
* @return {?}
*/
getCurrentLocale() {
/** @type {?} */
let locale = this.defaultLocale.languageCode;
locale += !!this.defaultLocale.scriptCode ? "-" + this.defaultLocale.scriptCode : "";
locale += !!this.defaultLocale.countryCode ? "-" + this.defaultLocale.countryCode : "";
return locale;
}
/**
* @return {?}
*/
getCurrentNumberingSystem() {
return this.defaultLocale.numberingSystem || "";
}
/**
* @return {?}
*/
getCurrentCalendar() {
return this.defaultLocale.calendar || "";
}
/**
* @return {?}
*/
getDefaultLocale() {
return this.defaultLocale.value;
}
/**
* @return {?}
*/
getCurrentCurrency() {
return this.currencyCode;
}
/**
* @param {?} currencyDisplay
* @param {?=} defaultLocale
* @param {?=} currency
* @return {?}
*/
getCurrencySymbol(currencyDisplay, defaultLocale, currency) {
/** @type {?} */
let currencySymbol = this.currencyCode;
if (IntlAPI.hasNumberFormat()) {
/** @type {?} */
const localeZero = this.formatDecimal(0, '1.0-0', defaultLocale);
/** @type {?} */
const localeValue = this.formatCurrency(0, '1.0-0', currencyDisplay, defaultLocale, currency);
currencySymbol = localeValue.replace(localeZero, "");
currencySymbol = currencySymbol.trim();
}
return currencySymbol;
}
/**
* @return {?}
*/
getCurrentTimezone() {
return this.timezone;
}
/**
* @param {?} languageCode
* @return {?}
*/
setCurrentLanguage(languageCode) {
if (this.defaultLocale.languageCode != languageCode) {
this.rollbackLanguageCode = this.defaultLocale.languageCode;
this.defaultLocale.build(languageCode);
this.releaseLanguage();
}
}
/**
* @param {?} languageCode
* @param {?=} countryCode
* @param {?=} scriptCode
* @param {?=} numberingSystem
* @param {?=} calendar
* @return {?}
*/
setDefaultLocale(languageCode, countryCode, scriptCode, numberingSystem, calendar) {
if (this.defaultLocale.languageCode != languageCode ||
this.defaultLocale.countryCode != countryCode ||
this.defaultLocale.scriptCode != scriptCode ||
this.defaultLocale.numberingSystem != numberingSystem ||
this.defaultLocale.calendar != calendar) {
this.rollbackDefaultLocale = this.defaultLocale.value;
this.defaultLocale.build(languageCode, countryCode, scriptCode, numberingSystem, calendar);
this.releaseDefaultLocale();
}
}
/**
* @param {?} currencyCode
* @return {?}
*/
setCurrentCurrency(currencyCode) {
if (this.currencyCode != currencyCode) {
this.rollbackCurrencyCode = this.currencyCode;
this.currencyCode = currencyCode;
this.releaseCurrency();
}
}
/**
* @param {?} zoneName
* @return {?}
*/
setCurrentTimezone(zoneName) {
if (this.timezone != zoneName) {
this.rollbackTimezone = this.timezone;
this.timezone = zoneName;
this.releaseTimezone();
}
}
/**
* Formats a date according to default locale.
* @param {?} value A Date, a number (milliseconds since UTC epoch) or an ISO string
* @param {?=} format An alias or a DateTimeOptions object of the format. Default is 'mediumDate'
* @param {?=} defaultLocale The default locale to use. Default is the current locale
* @param {?=} timezone The time zone name. Default is the current timezone
* @return {?}
*/
formatDate(value, format, defaultLocale, timezone) {
return IntlFormatter.formatDate(value, defaultLocale || this.defaultLocale.value, format || 'mediumDate', timezone || this.timezone);
}
/**
* Formats a relative time according to default locale.
* @param {?} value Negative (or positive) number
* @param {?} unit Unit of the value. Possible values are: 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second'
* @param {?=} format RelativeTimeOptions object of the format
* @param {?=} defaultLocale The default locale to use. Default is the current locale
* @return {?}
*/
formatRelativeTime(value, unit, format, defaultLocale) {
return IntlFormatter.formatRelativeTime(value, unit, defaultLocale || this.defaultLocale.value, format);
}
/**
* Formats a decimal number according to default locale.
* @param {?} value The number to be formatted
* @param {?=} digits An alias or a DigitsOptions object of the format
* @param {?=} defaultLocale The default locale to use. Default is the current locale
* @return {?}
*/
formatDecimal(value, digits, defaultLocale) {
return IntlFormatter.formatNumber(value, defaultLocale || this.defaultLocale.value, NumberFormatStyle.Decimal, digits);
}
/**
* Formats a number as a percentage according to default locale.
* @param {?} value The number to be formatted
* @param {?=} digits An alias or a DigitsOptions object of the format
* @param {?=} defaultLocale The default locale to use. Default is the current locale
* @return {?}
*/
formatPercent(value, digits, defaultLocale) {
return IntlFormatter.formatNumber(value, defaultLocale || this.defaultLocale.value, NumberFormatStyle.Percent, digits);
}
/**
* Formats a number as a currency according to default locale.
* @param {?} value The number to be formatted
* @param {?=} digits An alias or a DigitsOptions object of the format
* @param {?=} currencyDisplay The format for the currency. Possible values are 'code', 'symbol', 'name'. Default is 'symbol'
* @param {?=} defaultLocale The default locale to use. Default is the current locale
* @param {?=} currency The currency to use. Default is the current currency
* @return {?}
*/
formatCurrency(value, digits, currencyDisplay, defaultLocale, currency) {
return IntlFormatter.formatNumber(value, defaultLocale || this.defaultLocale.value, NumberFormatStyle.Currency, digits, currency || this.currencyCode, currencyDisplay || 'symbol');
}
/**
* @param {?} codes
* @return {?}
*/
composeLocale(codes) {
/** @type {?} */
let locale = "";
if (this.defaultLocale.languageCode) {
for (const code of codes) {
switch (code) {
case ISOCode.Script:
locale += "-" + this.defaultLocale.scriptCode;
break;
case ISOCode.Country:
locale += "-" + this.defaultLocale.countryCode;
break;
default:
locale += this.defaultLocale.languageCode;
}
}
}
return locale;
}
/**
* Rollbacks to previous language, default locale, currency & timezone.
* @return {?}
*/
rollback() {
if (this.rollbackLanguageCode && this.rollbackLanguageCode != this.defaultLocale.languageCode) {
this.defaultLocale.value = this.rollbackLanguageCode;
this.releaseLanguage();
}
if (this.rollbackDefaultLocale && this.rollbackDefaultLocale != this.defaultLocale.value) {
this.defaultLocale.value = this.rollbackDefaultLocale;
this.releaseDefaultLocale();
}
if (this.rollbackCurrencyCode && this.rollbackCurrencyCode != this.currencyCode) {
this.currencyCode = this.rollbackCurrencyCode;
this.releaseCurrency();
}
if (this.rollbackTimezone && this.rollbackTimezone != this.timezone) {
this.timezone = this.rollbackTimezone;
this.releaseTimezone();
}
}
/**
* @return {?}
*/
initLanguage() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.configuration.locale.language) {
if (!this.defaultLocale.languageCode) {
// Tries to get the language from the storage.
/** @type {?} */
const defaultLocale = yield this.storage.read("defaultLocale");
if (!!defaultLocale) {
this.defaultLocale.value = defaultLocale;
}
else {
// Tries to get the language from the browser.
/** @type {?} */
const browserLanguage = this.getBrowserLanguage();
/** @type {?} */
const matchedLanguage = this.matchLanguage(browserLanguage);
if (!!browserLanguage && matchedLanguage) {
this.defaultLocale.build(browserLanguage);
}
else {
// Uses the language set in the configuration.
this.defaultLocale.build(this.configuration.locale.language);
}
this.storage.write("defaultLocale", this.defaultLocale.value);
}
this.rollbackLanguageCode = this.defaultLocale.languageCode;
this.sendLanguageEvents();
}
}
});
}
/**
* @return {?}
*/
initDefaultLocale() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.configuration.locale.defaultLocale) {
if (!this.defaultLocale.value) {
/** @type {?} */
const defaultLocale = yield this.storage.read("defaultLocale");
if (!!defaultLocale) {
this.defaultLocale.value = defaultLocale;
}
else {
this.defaultLocale.build(this.configuration.locale.defaultLocale.languageCode, this.configuration.locale.defaultLocale.countryCode, this.configuration.locale.defaultLocale.scriptCode, this.configuration.locale.defaultLocale.numberingSystem, this.configuration.locale.defaultLocale.calendar);
this.storage.write("defaultLocale", this.defaultLocale.value);
}
this.rollbackDefaultLocale = this.defaultLocale.value;
this.sendDefaultLocaleEvents();
}
}
});
}
/**
* @return {?}
*/
initCurrency() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.configuration.locale.currency) {
if (!this.currencyCode) {
/** @type {?} */
const currencyCode = yield this.storage.read("currency");
if (!!currencyCode) {
this.currencyCode = currencyCode;
}
else {
this.currencyCode = this.configuration.locale.currency;
this.storage.write("currency", this.currencyCode);
}
this.rollbackCurrencyCode = this.currencyCode;
this.sendCurrencyEvents();
}
}
});
}
/**
* @return {?}
*/
initTimezone() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.configuration.locale.timezone) {
if (!this.timezone) {
/** @type {?} */
const zoneName = yield this.storage.read("timezone");
if (!!zoneName) {
this.timezone = zoneName;
}
else {
this.timezone = this.configuration.locale.timezone;
this.storage.write("timezone", this.timezone);
}
this.rollbackTimezone = this.timezone;
this.sendTimezoneEvents();
}
}
});
}
/**
* @param {?} languageCode
* @return {?}
*/
matchLanguage(languageCode) {
/** @type {?} */
let matchedLanguage;
if (this.configuration.locale.languages && languageCode != null) {
matchedLanguage = this.configuration.locale.languages.find((language) => language.code == languageCode);
}
return matchedLanguage;
}
/**
* @return {?}
*/
releaseLanguage() {
this.storage.write("defaultLocale", this.defaultLocale.value);
this.sendLanguageEvents();
}
/**
* @return {?}
*/
releaseDefaultLocale() {
this.storage.write("defaultLocale", this.defaultLocale.value);
this.sendDefaultLocaleEvents();
}
/**
* @return {?}
*/
releaseCurrency() {
this.storage.write("currency", this.currencyCode);
this.sendCurrencyEvents();
}
/**
* @return {?}
*/
releaseTimezone() {
this.storage.write("timezone", this.timezone);
this.sendTimezoneEvents();
}
/**
* @return {?}
*/
sendLanguageEvents() {
this.languageCodeChanged.next(this.defaultLocale.languageCode);
}
/**
* @return {?}
*/
sendDefaultLocaleEvents() {
this.defaultLocaleChanged.next(this.defaultLocale.value);
}
/**
* @return {?}
*/
sendCurrencyEvents() {
this.currencyCodeChanged.next(this.currencyCode);
}
/**
* @return {?}
*/
sendTimezoneEvents() {
this.timezoneChanged.next(this.timezone);
}
}
LocaleService.decorators = [
{ type: Injectable }
];
/** @nocollapse */
LocaleService.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [L10N_CONFIG,] }] },
{ type: LocaleStorage }
];
if (false) {
/**
* Fired when the language changes. Returns the language code.
* @type {?}
*/
LocaleService.prototype.languageCodeChanged;
/**
* Fired when the default locale changes. Returns the default locale.
* @type {?}
*/
LocaleService.prototype.defaultLocaleChanged;
/**
* Fired when the currency changes. Returns the currency code.
* @type {?}
*/
LocaleService.prototype.currencyCodeChanged;
/**
* Fired when the timezone changes. Returns the timezone.
* @type {?}
*/
LocaleService.prototype.timezoneChanged;
/** @type {?} */
LocaleService.prototype.defaultLocale;
/** @type {?} */
LocaleService.prototype.currencyCode;
/** @type {?} */
LocaleService.prototype.timezone;
/** @type {?} */
LocaleService.prototype.rollbackLanguageCode;
/** @type {?} */
LocaleService.prototype.rollbackDefaultLocale;
/** @type {?} */
LocaleService.prototype.rollbackCurrencyCode;
/** @type {?} */
LocaleService.prototype.rollbackTimezone;
/** @type {?} */
LocaleService.prototype.configuration;
/** @type {?} */
LocaleService.prototype.storage;
}
//# sourceMappingURL=locale.service.js.map