Refactor language import in I18nService

This commit is contained in:
MathieuG-P
2023-12-15 21:10:49 +01:00
parent 220111d81f
commit f76e693bb7
+16 -2
View File
@@ -33,9 +33,9 @@ export class I18nService {
filter(l => !!l),
distinctUntilChanged()
)
.subscribe(lang => {
.subscribe(async lang => {
this.cache.clear();
this.dictionary = require(`../../../assets/jsons/translations/${lang.split("-")[0]}.json`);
this.dictionary = this.importLang([lang, lang.split("-")[0]], "en");
i18n.dayNames = getProperty(this.dictionary, "dateformat.dayNames");
i18n.monthNames = getProperty(this.dictionary, "dateformat.monthNames");
@@ -43,6 +43,20 @@ export class I18nService {
});
}
private importLang(lang: string[], fallback: string): Record<string, string> {
for (const l of lang) {
try {
return require(`../../../assets/jsons/translations/${l.toLowerCase()}.json`);
}
catch (e) {
continue;
}
}
return require(`../../../assets/jsons/translations/${fallback.toLowerCase()}.json`);
}
public getSupportedLanguages(): string[] {
return this.configService.get("supported_languages" as DefaultConfigKey);
}