ngx-translate-cache
Version:
ngx-translate extension to facilitate language cache.
154 lines (148 loc) • 6.86 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Inject, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
var CacheMechanism;
(function (CacheMechanism) {
CacheMechanism.LocalStorage = 'LocalStorage';
CacheMechanism.Cookie = 'Cookie';
})(CacheMechanism || (CacheMechanism = {}));
const CACHE_NAME = new InjectionToken('CACHE_NAME');
const CACHE_MECHANISM = new InjectionToken('CACHE_MECHANISM');
const COOKIE_EXPIRY = new InjectionToken('COOKIE_EXPIRY');
const COOKIE_ATTRIBUTES = new InjectionToken('COOKIE_ATTRIBUTES');
const DEFAULT_CACHE_NAME = 'lang';
const DEFAULT_CACHE_MECHANISM = CacheMechanism.LocalStorage;
const DEFAULT_COOKIE_EXPIRY = 720;
class TranslateCacheSettings {
constructor(cacheName = DEFAULT_CACHE_NAME, cacheMechanism = DEFAULT_CACHE_MECHANISM, cookieExpiry = DEFAULT_COOKIE_EXPIRY, cookieAttributes) {
this.cacheName = cacheName;
this.cacheMechanism = cacheMechanism;
this.cookieExpiry = cookieExpiry;
this.cookieAttributes = cookieAttributes;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.8", ngImport: i0, type: TranslateCacheSettings, deps: [{ token: CACHE_NAME }, { token: CACHE_MECHANISM }, { token: COOKIE_EXPIRY }, { token: COOKIE_ATTRIBUTES }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.8", ngImport: i0, type: TranslateCacheSettings }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.8", ngImport: i0, type: TranslateCacheSettings, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: undefined, decorators: [{
type: Inject,
args: [CACHE_NAME]
}] }, { type: undefined, decorators: [{
type: Inject,
args: [CACHE_MECHANISM]
}] }, { type: undefined, decorators: [{
type: Inject,
args: [COOKIE_EXPIRY]
}] }, { type: undefined, decorators: [{
type: Inject,
args: [COOKIE_ATTRIBUTES]
}] }] });
/* Not injectable */
class TranslateCacheService {
constructor(translateService, translateCacheSettings) {
this.translateService = translateService;
this.translateCacheSettings = translateCacheSettings;
}
init() {
this.translateService.onLangChange
.subscribe((event) => {
if (this.translateCacheSettings.cacheMechanism === CacheMechanism.LocalStorage) {
return this.cacheWithLocalStorage(event.lang);
}
if (this.translateCacheSettings.cacheMechanism === CacheMechanism.Cookie) {
return this.cacheWithCookies(event.lang);
}
});
const currentLang = this.getCachedLanguage() || this.translateService.getBrowserLang();
if (currentLang) {
this.translateService.use(currentLang);
}
}
getCachedLanguage() {
if (this.translateCacheSettings.cacheMechanism === CacheMechanism.LocalStorage) {
return this.cacheWithLocalStorage();
}
if (this.translateCacheSettings.cacheMechanism === CacheMechanism.Cookie) {
return this.cacheWithCookies();
}
}
cacheWithLocalStorage(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;
}
}
cacheWithCookies(value) {
if (typeof document === 'undefined' || typeof document.cookie === 'undefined') {
return;
}
try {
const name = encodeURIComponent(this.translateCacheSettings.cacheName);
if (value) {
let cookieString = `${name}=${encodeURIComponent(value)}`;
if (this.translateCacheSettings.cookieExpiry >= 0) {
const date = new Date();
date.setTime(date.getTime() + this.translateCacheSettings.cookieExpiry * 3600000);
cookieString += `;expires=${date.toUTCString()}`;
}
if (this.translateCacheSettings.cookieAttributes) {
cookieString += ';' + this.translateCacheSettings.cookieAttributes;
}
document.cookie = cookieString;
return;
}
const regexp = new RegExp('(?:^' + name + '|;\\s*' + name + ')=(.*?)(?:;|$)', 'g');
const result = regexp.exec(document.cookie);
return decodeURIComponent(result[1]);
}
catch (e) {
return;
}
}
}
class TranslateCacheModule {
static forRoot(config) {
return {
ngModule: TranslateCacheModule,
providers: [
{ provide: CACHE_NAME, useValue: config.cacheName },
{ provide: CACHE_MECHANISM, useValue: config.cacheMechanism },
{ provide: COOKIE_EXPIRY, useValue: config.cookieExpiry },
{ provide: COOKIE_ATTRIBUTES, useValue: config.cookieAttributes },
TranslateCacheSettings,
config.cacheService,
]
};
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.8", ngImport: i0, type: TranslateCacheModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.0.8", ngImport: i0, type: TranslateCacheModule, imports: [CommonModule] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.0.8", ngImport: i0, type: TranslateCacheModule, imports: [CommonModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.8", ngImport: i0, type: TranslateCacheModule, decorators: [{
type: NgModule,
args: [{
imports: [
CommonModule
],
declarations: [],
exports: []
}]
}] });
/*
* Public API Surface of ngx-translate-cache
*/
/**
* Generated bundle index. Do not edit.
*/
export { CACHE_MECHANISM, CACHE_NAME, COOKIE_ATTRIBUTES, COOKIE_EXPIRY, CacheMechanism, TranslateCacheModule, TranslateCacheService, TranslateCacheSettings };
//# sourceMappingURL=ngx-translate-cache.mjs.map