Allow loading language files with two part language code (#339)

* Change the imported filenames to inlcude underscore in two part language names. (issue #19218)

Signed-off-by: Timo Piiroinen <timo.piiroinen@unikie.com>

* Refactoring the code

Signed-off-by: Timo Piiroinen <timo.piiroinen@unikie.com>

* Refactored code and added comment

Signed-off-by: Timo Piiroinen <timo.piiroinen@unikie.com>
This commit is contained in:
Timo Piiroinen 2022-04-22 14:16:04 +03:00 committed by GitHub
parent 4d67e0561d
commit fcfda67511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -92,10 +92,22 @@ export class AppLocalization {
this.resetLocalizedUI();
}
// Format language strings from normalized form to non-normalized form (e.g. en-gb to en_GB)
private denormalize(locale: string): string {
if (locale === "en") {
locale = "en_EN";
}
const parts = locale.split("-");
if (parts.length > 1) {
parts[1] = parts[1].toUpperCase();
}
return parts.join("_");
}
public fetchTranslationJson(locale: string): Record<string, string> {
try {
console.log("Fetching translation json for locale: " + locale);
return require(`./i18n/strings/${locale}.json`);
return require(`./i18n/strings/${this.denormalize(locale)}.json`);
} catch (e) {
console.log(`Could not fetch translation json for locale: '${locale}'`, e);
return null;