Skip to content

Commit

Permalink
Allow loading language files with two part language code (#339)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
TPiUnikie authored Apr 22, 2022
1 parent 4d67e05 commit fcfda67
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/language-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fcfda67

Please sign in to comment.