angular-l10n
Version:
An Angular library to translate messages, dates and numbers
113 lines • 4.13 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 { L10N_CONFIG } from "../models/l10n-config";
import { StorageStrategy } from '../models/types';
import { getLocalStorage, getSessionStorage, getCookie, setLocalStorage, setSessionStorage, setCookie } from '../models/utils';
/**
* Implement this class-interface to create a custom storage for default locale, currency & timezone.
* @abstract
*/
export class LocaleStorage {
}
LocaleStorage.decorators = [
{ type: Injectable }
];
if (false) {
/**
* This method must contain the logic to read the storage.
* @abstract
* @param {?} name 'defaultLocale', 'currency' or 'timezone'
* @return {?} A promise with the value of the given name
*/
LocaleStorage.prototype.read = function (name) { };
/**
* This method must contain the logic to write the storage.
* @abstract
* @param {?} name 'defaultLocale', 'currency' or 'timezone'
* @param {?} value The value for the given name
* @return {?}
*/
LocaleStorage.prototype.write = function (name, value) { };
}
export class L10nStorage {
/**
* @param {?} configuration
*/
constructor(configuration) {
this.configuration = configuration;
this.hasCookie = typeof navigator !== "undefined" && navigator.cookieEnabled;
this.hasStorage = typeof Storage !== "undefined";
}
/**
* @param {?} name
* @return {?}
*/
read(name) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
/** @type {?} */
let value = null;
if (this.configuration.locale.storage != StorageStrategy.Disabled) {
if (this.configuration.locale.storage == StorageStrategy.Local && this.hasStorage) {
value = getLocalStorage(this.getName(name));
}
else if (this.configuration.locale.storage == StorageStrategy.Session && this.hasStorage) {
value = getSessionStorage(this.getName(name));
}
else if (this.configuration.locale.storage == StorageStrategy.Cookie && this.hasCookie) {
value = getCookie(this.getName(name));
}
}
return value;
});
}
/**
* @param {?} name
* @param {?} value
* @return {?}
*/
write(name, value) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.configuration.locale.storage != StorageStrategy.Disabled) {
if (this.configuration.locale.storage == StorageStrategy.Local && this.hasStorage) {
setLocalStorage(this.getName(name), value);
}
else if (this.configuration.locale.storage == StorageStrategy.Session && this.hasStorage) {
setSessionStorage(this.getName(name), value);
}
else if (this.configuration.locale.storage == StorageStrategy.Cookie && this.hasCookie) {
setCookie(this.getName(name), value, this.configuration.locale.cookieExpiration);
}
}
});
}
/**
* @param {?} name
* @return {?}
*/
getName(name) {
if (this.configuration.locale.storageNames) {
return ((/** @type {?} */ (this.configuration.locale.storageNames)))[name] || name;
}
return name;
}
}
L10nStorage.decorators = [
{ type: Injectable }
];
/** @nocollapse */
L10nStorage.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [L10N_CONFIG,] }] }
];
if (false) {
/** @type {?} */
L10nStorage.prototype.hasCookie;
/** @type {?} */
L10nStorage.prototype.hasStorage;
/** @type {?} */
L10nStorage.prototype.configuration;
}
//# sourceMappingURL=locale-storage.js.map