UNPKG

ngx-translate-cache

Version:

ngx-translate extension to facilitate language cache.

171 lines (165 loc) 6.53 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common')) : typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/common'], factory) : (factory((global['ngx-translate-cache'] = {}),global.core,global.common)); }(this, (function (exports,core,common) { 'use strict'; (function (CacheMechanism) { CacheMechanism.LocalStorage = 'LocalStorage'; CacheMechanism.Cookie = 'Cookie'; })(exports.CacheMechanism || (exports.CacheMechanism = {})); var CACHE_NAME = new core.InjectionToken('CACHE_NAME'); var CACHE_MECHANISM = new core.InjectionToken('CACHE_MECHANISM'); var COOKIE_EXPIRY = new core.InjectionToken('COOKIE_EXPIRY'); var DEFAULT_CACHE_NAME = 'lang'; var DEFAULT_CACHE_MECHANISM = exports.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: core.Inject, args: [CACHE_NAME,] },] }, { type: undefined, decorators: [{ type: core.Inject, args: [CACHE_MECHANISM,] },] }, { type: undefined, decorators: [{ type: core.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 === exports.CacheMechanism.LocalStorage) { return _this.cacheWithLocalStorage(event.lang); } if (_this.translateCacheSettings.cacheMechanism === exports.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 === exports.CacheMechanism.LocalStorage) { return this.cacheWithLocalStorage(); } if (this.translateCacheSettings.cacheMechanism === exports.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: core.NgModule, args: [{ imports: [ common.CommonModule ], declarations: [], exports: [] },] }, ]; /** * @nocollapse */ TranslateCacheModule.ctorParameters = function () { return []; }; exports.TranslateCacheModule = TranslateCacheModule; exports.CACHE_NAME = CACHE_NAME; exports.CACHE_MECHANISM = CACHE_MECHANISM; exports.COOKIE_EXPIRY = COOKIE_EXPIRY; exports.TranslateCacheSettings = TranslateCacheSettings; exports.TranslateCacheService = TranslateCacheService; Object.defineProperty(exports, '__esModule', { value: true }); })));