-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use locale storage to cache translations. Co-Authored-by: Bertrand Zuchuat <[email protected]>
- Loading branch information
1 parent
e368767
commit 475b231
Showing
7 changed files
with
249 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Observable } from "rxjs"; | ||
|
||
/* | ||
* RERO angular core | ||
* Copyright (C) 2020-2023 RERO | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
export interface ICache { | ||
set(key: string, data: any): this; | ||
get(key: string): Observable<any> | undefined; | ||
} |
81 changes: 81 additions & 0 deletions
81
projects/rero/ng-core/src/lib/translate/translate-cache.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* RERO angular core | ||
* Copyright (C) 2020-2023 RERO | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { TranslateCacheService } from './translate-cache.service'; | ||
import { CoreConfigService } from '../core-config.service'; | ||
import { LocalStorageService } from '../service/local-storage.service'; | ||
|
||
class AppConfigService extends CoreConfigService { | ||
constructor() { | ||
super(); | ||
this.appVersion = '1.0.0'; | ||
} | ||
} | ||
|
||
export const translations = { | ||
foo: 'bar' | ||
}; | ||
|
||
export const historyLocaleStorage1 = { | ||
version: '1.0.0', | ||
storageName: ['translations_fr', 'translations_de'] | ||
}; | ||
|
||
export const historyLocaleStorage11 = { | ||
version: '1.0.1', | ||
storageName: [] | ||
}; | ||
|
||
describe('TranslateCacheService', () => { | ||
let service: TranslateCacheService; | ||
let localeStorage: LocalStorageService; | ||
let appConfigService: AppConfigService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
{ provide: CoreConfigService, useClass: AppConfigService } | ||
] | ||
}); | ||
service = TestBed.inject(TranslateCacheService); | ||
localeStorage = TestBed.inject(LocalStorageService); | ||
appConfigService = TestBed.inject(CoreConfigService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('Should return the correct cache values', () => { | ||
localeStorage.clear(); | ||
expect(service.get('fr')).toBeUndefined(); | ||
service.set('fr', translations); | ||
expect(service.get('fr')).toEqual(translations); | ||
expect(service.get('de')).toBeUndefined(); | ||
service.set('de', translations); | ||
const history = localeStorage.get(service.translateHistoryName); | ||
expect(history).toEqual(historyLocaleStorage1); | ||
const {storageName} = history; | ||
storageName.forEach((name: string) => expect(localeStorage.has(name)).toBeTrue()); | ||
// Application version change | ||
// The translation cache is cleared | ||
appConfigService.appVersion = '1.0.1'; | ||
expect(service.get('fr')).toBeUndefined(); | ||
expect(localeStorage.get(service.translateHistoryName)).toEqual(historyLocaleStorage11); | ||
storageName.forEach((name: string) => expect(localeStorage.has(name)).toBeFalse()); | ||
}); | ||
}); |
120 changes: 120 additions & 0 deletions
120
projects/rero/ng-core/src/lib/translate/translate-cache.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* RERO angular core | ||
* Copyright (C) 2020-2023 RERO | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, version 3 of the License. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { Injectable } from '@angular/core'; | ||
import { CoreConfigService } from '../core-config.service'; | ||
import { ICache } from '../interface/icache'; | ||
import { LocalStorageService } from '../service/local-storage.service'; | ||
|
||
export interface ITranslations { | ||
version: string; | ||
storageName: string[]; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class TranslateCacheService implements ICache { | ||
|
||
/** Name of local storage history */ | ||
readonly translateHistoryName: string = 'translations'; | ||
|
||
/** | ||
* Constructor | ||
* @param localeStorage LocalStorageService | ||
* @param coreConfigService CoreConfigService | ||
*/ | ||
constructor( | ||
private localeStorage: LocalStorageService, | ||
private coreConfigService: CoreConfigService | ||
) { | ||
if (this.coreConfigService.appVersion === undefined) { | ||
throw new Error('Please set your application version to use the cache.'); | ||
} | ||
} | ||
|
||
/** | ||
* Adding content to locale storage | ||
* @param key - Locale storage key | ||
* @param data - Locale storage contents | ||
* @returns TranslateCacheService | ||
*/ | ||
set(key: string, data: any): this { | ||
const history = this.localeStorage.has(this.translateHistoryName) | ||
? this.localeStorage.get(this.translateHistoryName) | ||
: this.initializeTranslationsHistory(); | ||
const name = this.storageName(key); | ||
if (!history.storageName.includes(name)) { | ||
history.storageName.push(name); | ||
this.localeStorage.set(this.translateHistoryName, history); | ||
} | ||
this.localeStorage.set(name, data); | ||
return this; | ||
} | ||
|
||
/** | ||
* Retrieving content from locale storage | ||
* @param key - Locale storage key | ||
* @returns The contents of the locale storage or undefined | ||
*/ | ||
get(key: string): any | undefined { | ||
if (this.localeStorage.has(this.translateHistoryName)) { | ||
const history = this.localeStorage.get(this.translateHistoryName); | ||
if (history.version !== this.coreConfigService.appVersion) { | ||
this.resetTranslationsHistory(history); | ||
} else { | ||
const name = this.storageName(key); | ||
if (history.storageName.includes(name) && this.localeStorage.has(name)) { | ||
return this.localeStorage.get(name); | ||
} | ||
} | ||
} | ||
return; | ||
} | ||
|
||
/** | ||
* Key generation for translations | ||
* @param language - current language | ||
* @returns Key name for locale storage | ||
*/ | ||
private storageName(language: string): string { | ||
return `${this.translateHistoryName}_${language}`; | ||
} | ||
|
||
/** | ||
* Delete languages in locale storage and reset object history. | ||
* @param history - history object (ITranslations) | ||
*/ | ||
private resetTranslationsHistory(history: ITranslations): void { | ||
history.storageName.forEach(name => { | ||
if (this.localeStorage.has(name)) { | ||
this.localeStorage.remove(name); | ||
} | ||
}); | ||
this.localeStorage.set(this.translateHistoryName, this.initializeTranslationsHistory()); | ||
} | ||
|
||
/** | ||
* Generate object history stored in locale storage. | ||
* @returns - ITranslations | ||
*/ | ||
private initializeTranslationsHistory(): ITranslations { | ||
return { | ||
version: this.coreConfigService.appVersion, | ||
storageName: [] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters