add cache

This commit is contained in:
MathieuG-P
2022-07-13 16:22:17 +02:00
parent e6a9b1ff11
commit 8f980e962c
+38 -30
View File
@@ -5,45 +5,53 @@ import { getProperty } from "dot-prop";
export class I18nService {
private static instance: I18nService;
private static instance: I18nService;
private readonly LANG_CONFIG_KEY: DefaultConfigKey = "language";
private readonly LANG_FALLBACK = "en-EN";
private readonly cache: Map<string, string>;
private readonly configService: ConfigurationService;
private readonly LANG_CONFIG_KEY: DefaultConfigKey = "language";
private readonly LANG_FALLBACK = "en-EN";
private dictionary: Object;
private readonly configService: ConfigurationService;
public static getInstance(): I18nService{
if(!I18nService.instance){ I18nService.instance = new I18nService(); }
return I18nService.instance;
private dictionary: Object;
public static getInstance(): I18nService{
if(!I18nService.instance){ I18nService.instance = new I18nService(); }
return I18nService.instance;
}
private constructor(){
this.configService = ConfigurationService.getInstance();
this.cache = new Map<string, string>();
this.currentLanguage$.pipe(filter(l => !!l), distinctUntilChanged()).subscribe(lang => {
this.cache.clear();
this.dictionary = require(`../../../assets/jsons/translations/${lang}.json`);
});
}
private constructor(){
this.configService = ConfigurationService.getInstance();
public getSupportedLanguages(): string[]{ return this.configService.get("supported_languages" as DefaultConfigKey); }
public getFallbackLanguage(): string{ return this.LANG_FALLBACK; }
this.currentLanguage$.pipe(filter(l => !!l), distinctUntilChanged()).subscribe(lang => {
this.dictionary = require(`../../../assets/jsons/translations/${lang}.json`);
});
}
public get currentLanguage(): string{
return this.getSupportedLanguages().includes(this.configService.get(this.LANG_CONFIG_KEY)) ? this.configService.get(this.LANG_CONFIG_KEY) : this.LANG_FALLBACK;
}
public get currentLanguage$(): Observable<string>{
return this.configService.watch<string>("language" as DefaultConfigKey).pipe(map(l => this.getSupportedLanguages().includes(l) ? l : this.LANG_FALLBACK));
}
public getSupportedLanguages(): string[]{ return this.configService.get("supported_languages" as DefaultConfigKey); }
public getFallbackLanguage(): string{ return this.LANG_FALLBACK; }
public setLanguage(lang: string){
this.configService.set("language" as DefaultConfigKey, this.getSupportedLanguages().includes(lang) ? lang : this.LANG_FALLBACK);
}
public get currentLanguage(): string{
return this.getSupportedLanguages().includes(this.configService.get(this.LANG_CONFIG_KEY)) ? this.configService.get(this.LANG_CONFIG_KEY) : this.LANG_FALLBACK;
}
public get currentLanguage$(): Observable<string>{
return this.configService.watch<string>("language" as DefaultConfigKey).pipe(map(l => this.getSupportedLanguages().includes(l) ? l : this.LANG_FALLBACK));
}
public setLanguage(lang: string){
this.configService.set("language" as DefaultConfigKey, this.getSupportedLanguages().includes(lang) ? lang : this.LANG_FALLBACK);
}
public translate(translationKey: string): string{
return getProperty(this.dictionary, translationKey) || translationKey;
}
public translate(translationKey: string): string{
const cachedTranlation = this.cache.get(translationKey);
if(!!cachedTranlation){ return cachedTranlation; }
console.log(translationKey);
const tranlated = getProperty(this.dictionary, translationKey);
tranlated && this.cache.set(translationKey, tranlated);
return tranlated || translationKey;
}