angular-l10n
Version:
An Angular library to translate messages, dates and numbers
134 lines • 4.95 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
*/
var LocaleStorage = /** @class */ (function () {
function LocaleStorage() {
}
LocaleStorage.decorators = [
{ type: Injectable }
];
return LocaleStorage;
}());
export { LocaleStorage };
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) { };
}
var L10nStorage = /** @class */ (function () {
function L10nStorage(configuration) {
this.configuration = configuration;
this.hasCookie = typeof navigator !== "undefined" && navigator.cookieEnabled;
this.hasStorage = typeof Storage !== "undefined";
}
/**
* @param {?} name
* @return {?}
*/
L10nStorage.prototype.read = /**
* @param {?} name
* @return {?}
*/
function (name) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var value;
return tslib_1.__generator(this, function (_a) {
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 [2 /*return*/, value];
});
});
};
/**
* @param {?} name
* @param {?} value
* @return {?}
*/
L10nStorage.prototype.write = /**
* @param {?} name
* @param {?} value
* @return {?}
*/
function (name, value) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
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);
}
}
return [2 /*return*/];
});
});
};
/**
* @param {?} name
* @return {?}
*/
L10nStorage.prototype.getName = /**
* @param {?} name
* @return {?}
*/
function (name) {
if (this.configuration.locale.storageNames) {
return ((/** @type {?} */ (this.configuration.locale.storageNames)))[name] || name;
}
return name;
};
L10nStorage.decorators = [
{ type: Injectable }
];
/** @nocollapse */
L10nStorage.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [L10N_CONFIG,] }] }
]; };
return L10nStorage;
}());
export { L10nStorage };
if (false) {
/** @type {?} */
L10nStorage.prototype.hasCookie;
/** @type {?} */
L10nStorage.prototype.hasStorage;
/** @type {?} */
L10nStorage.prototype.configuration;
}
//# sourceMappingURL=locale-storage.js.map