ngx-translate-cache
Version:
ngx-translate extension to facilitate language cache.
160 lines (156 loc) • 5.95 kB
JavaScript
import { Inject, InjectionToken, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
var CacheMechanism;
(function (CacheMechanism) {
CacheMechanism.LocalStorage = 'LocalStorage';
CacheMechanism.Cookie = 'Cookie';
})(CacheMechanism || (CacheMechanism = {}));
var CACHE_NAME = new InjectionToken('CACHE_NAME');
var CACHE_MECHANISM = new InjectionToken('CACHE_MECHANISM');
var COOKIE_EXPIRY = new InjectionToken('COOKIE_EXPIRY');
var DEFAULT_CACHE_NAME = 'lang';
var DEFAULT_CACHE_MECHANISM = CacheMechanism.LocalStorage;
var DEFAULT_COOKIE_EXPIRY = 720;
var TranslateCacheSettings = (function () {
/**
* @param {?=} cacheName
* @param {?=} cacheMechanism
* @param {?=} cookieExpiry
*/
function TranslateCacheSettings(cacheName, cacheMechanism, cookieExpiry) {
if (cacheName === void 0) { cacheName = DEFAULT_CACHE_NAME; }
if (cacheMechanism === void 0) { cacheMechanism = DEFAULT_CACHE_MECHANISM; }
if (cookieExpiry === void 0) { cookieExpiry = DEFAULT_COOKIE_EXPIRY; }
this.cacheName = cacheName;
this.cacheMechanism = cacheMechanism;
this.cookieExpiry = cookieExpiry;
}
return TranslateCacheSettings;
}());
/**
* @nocollapse
*/
TranslateCacheSettings.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [CACHE_NAME,] },] },
{ type: undefined, decorators: [{ type: Inject, args: [CACHE_MECHANISM,] },] },
{ type: undefined, decorators: [{ type: Inject, args: [COOKIE_EXPIRY,] },] },
]; };
var TranslateCacheService = (function () {
/**
* @param {?} translateService
* @param {?} translateCacheSettings
*/
function TranslateCacheService(translateService, translateCacheSettings) {
this.translateService = translateService;
this.translateCacheSettings = translateCacheSettings;
}
/**
* @return {?}
*/
TranslateCacheService.prototype.init = function () {
var _this = this;
this.translateService.onLangChange
.subscribe(function (event) {
if (_this.translateCacheSettings.cacheMechanism === CacheMechanism.LocalStorage) {
return _this.cacheWithLocalStorage(event.lang);
}
if (_this.translateCacheSettings.cacheMechanism === CacheMechanism.Cookie) {
return _this.cacheWithCookies(event.lang);
}
});
var /** @type {?} */ currentLang = this.getCachedLanguage() || this.translateService.getBrowserLang();
if (currentLang) {
this.translateService.use(currentLang);
}
};
/**
* @return {?}
*/
TranslateCacheService.prototype.getCachedLanguage = function () {
if (this.translateCacheSettings.cacheMechanism === CacheMechanism.LocalStorage) {
return this.cacheWithLocalStorage();
}
if (this.translateCacheSettings.cacheMechanism === CacheMechanism.Cookie) {
return this.cacheWithCookies();
}
};
/**
* @param {?=} value
* @return {?}
*/
TranslateCacheService.prototype.cacheWithLocalStorage = function (value) {
if (typeof window === 'undefined' || typeof window.localStorage === 'undefined') {
return;
}
try {
if (value) {
window.localStorage.setItem(this.translateCacheSettings.cacheName, value);
return;
}
return window.localStorage.getItem(this.translateCacheSettings.cacheName);
}
catch (e) {
return;
}
};
/**
* @param {?=} value
* @return {?}
*/
TranslateCacheService.prototype.cacheWithCookies = function (value) {
if (typeof document === 'undefined' || typeof document.cookie === 'undefined') {
return;
}
try {
var /** @type {?} */ name_1 = encodeURIComponent(this.translateCacheSettings.cacheName);
if (value) {
var /** @type {?} */ date = new Date();
date.setTime(date.getTime() + this.translateCacheSettings.cookieExpiry * 3600000);
document.cookie = name_1 + "=" + encodeURIComponent(value) + ";expires=" + date.toUTCString();
return;
}
var /** @type {?} */ regexp = new RegExp('(?:^' + name_1 + '|;\\s*' + name_1 + ')=(.*?)(?:;|$)', 'g');
var /** @type {?} */ result = regexp.exec(document.cookie);
return decodeURIComponent(result[1]);
}
catch (e) {
return;
}
};
return TranslateCacheService;
}());
var TranslateCacheModule = (function () {
function TranslateCacheModule() {
}
/**
* @param {?} config
* @return {?}
*/
TranslateCacheModule.forRoot = function (config) {
return {
ngModule: TranslateCacheModule,
providers: [
{ provide: CACHE_NAME, useValue: config.cacheName },
{ provide: CACHE_MECHANISM, useValue: config.cacheMechanism },
{ provide: COOKIE_EXPIRY, useValue: config.cookieExpiry },
TranslateCacheSettings,
config.cacheService,
]
};
};
return TranslateCacheModule;
}());
TranslateCacheModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule
],
declarations: [],
exports: []
},] },
];
/**
* @nocollapse
*/
TranslateCacheModule.ctorParameters = function () { return []; };
export { TranslateCacheModule, CacheMechanism, CACHE_NAME, CACHE_MECHANISM, COOKIE_EXPIRY, TranslateCacheSettings, TranslateCacheService };