-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add translations cache #563
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the purpose of using an interface. But it should work without any problem