diff --git a/src/renderer/services/i18n.service.ts b/src/renderer/services/i18n.service.ts index 05ffc81e..81deb10c 100644 --- a/src/renderer/services/i18n.service.ts +++ b/src/renderer/services/i18n.service.ts @@ -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; - 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(); + + 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{ + return this.configService.watch("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{ - return this.configService.watch("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; + }