From fcfda67511a71cf4b743212f07b14382cf4e87a0 Mon Sep 17 00:00:00 2001 From: Timo Piiroinen <102156385+TPiUnikie@users.noreply.github.com> Date: Fri, 22 Apr 2022 14:16:04 +0300 Subject: [PATCH] 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 * Refactoring the code Signed-off-by: Timo Piiroinen * Refactored code and added comment Signed-off-by: Timo Piiroinen --- src/language-helper.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/language-helper.ts b/src/language-helper.ts index cbf2aa2..47fe431 100644 --- a/src/language-helper.ts +++ b/src/language-helper.ts @@ -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 { 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;