From 5b9f5f07690cd0bb05d295dde3a882d430d9be46 Mon Sep 17 00:00:00 2001 From: Nils Hanff Date: Wed, 7 Feb 2024 16:59:42 +0100 Subject: [PATCH 1/4] Replace `{}` by `null` for missing locale AppLocalization.setAppLocale uses `!!translations` to check if a locale is avaliable, but `!!{} === true`. Thus, any localization was considered valid, even `{}`, which was returned from `AppLocalization.fetchTranslationJson` when no such locale was found. This does not happen when we return `null` since it is falsy. Signed-off-by: Nils Hanff --- src/language-helper.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/language-helper.ts b/src/language-helper.ts index 8d1df7cf3..892a5a35b 100644 --- a/src/language-helper.ts +++ b/src/language-helper.ts @@ -73,7 +73,7 @@ export class AppLocalization { private readonly localizedComponents?: Set; public constructor({ store, components = [] }: { store: TypedStore; components: Component[] }) { - counterpart.registerTranslations(FALLBACK_LOCALE, this.fetchTranslationJson("en_EN")); + counterpart.registerTranslations(FALLBACK_LOCALE, this.fetchTranslationJson("en_EN") ?? {}); counterpart.setFallbackLocale(FALLBACK_LOCALE); counterpart.setSeparator("|"); @@ -103,13 +103,13 @@ export class AppLocalization { return parts.join("_"); } - public fetchTranslationJson(locale: string): Record { + public fetchTranslationJson(locale: string): Record | null { try { console.log("Fetching translation json for locale: " + locale); return loadJsonFile(__dirname, "i18n", "strings", `${this.denormalize(locale)}.json`); } catch (e) { console.log(`Could not fetch translation json for locale: '${locale}'`, e); - return {}; + return null; } } From 7c71ac114c15202d07c9d59f97113b2eaca7e44d Mon Sep 17 00:00:00 2001 From: Nils Hanff Date: Wed, 7 Feb 2024 17:08:59 +0100 Subject: [PATCH 2/4] Add AppLocalization.variations The method returns suitable fallbacks in case a locale is not found. Signed-off-by: Nils Hanff --- src/language-helper.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/language-helper.ts b/src/language-helper.ts index 892a5a35b..16adc912d 100644 --- a/src/language-helper.ts +++ b/src/language-helper.ts @@ -91,11 +91,21 @@ export class AppLocalization { this.resetLocalizedUI(); } + // Fallbacks to a locale (e.g. en-gb to [en-gb, en, en-en] or just en to [en, en-en]) + private variations(locale: string): string[] { + const [genericPart, specificPart] = locale.split("-"); + const candidates = [locale]; + if (specificPart) { + candidates.push(genericPart); + } + if (genericPart != specificPart) { + candidates.push(`${genericPart}-${genericPart}`); + } + return candidates; + } + // 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(); @@ -120,7 +130,7 @@ export class AppLocalization { locales = [locales]; } - const loadedLocales = locales.filter((locale) => { + const loadedLocales = locales.flatMap(this.variations).filter((locale) => { const translations = this.fetchTranslationJson(locale); if (translations !== null) { counterpart.registerTranslations(locale, translations); From 2ca86f7918052721c4bdbc5ae6ea82de30f0a43a Mon Sep 17 00:00:00 2001 From: Nils Hanff Date: Wed, 7 Feb 2024 17:10:28 +0100 Subject: [PATCH 3/4] Use same locale name for logging and loading from file Signed-off-by: Nils Hanff --- src/language-helper.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/language-helper.ts b/src/language-helper.ts index 16adc912d..26cc19a4c 100644 --- a/src/language-helper.ts +++ b/src/language-helper.ts @@ -115,8 +115,8 @@ export class AppLocalization { public fetchTranslationJson(locale: string): Record | null { try { - console.log("Fetching translation json for locale: " + locale); - return loadJsonFile(__dirname, "i18n", "strings", `${this.denormalize(locale)}.json`); + console.log(`Fetching translation json for locale: ${locale}`); + return loadJsonFile(__dirname, "i18n", "strings", `${locale}.json`); } catch (e) { console.log(`Could not fetch translation json for locale: '${locale}'`, e); return null; @@ -130,7 +130,7 @@ export class AppLocalization { locales = [locales]; } - const loadedLocales = locales.flatMap(this.variations).filter((locale) => { + const loadedLocales = locales.flatMap(this.variations).map(this.denormalize).filter((locale) => { const translations = this.fetchTranslationJson(locale); if (translations !== null) { counterpart.registerTranslations(locale, translations); From 82a20371dce4c2e3f830d84ff57d7bcf0578e6bc Mon Sep 17 00:00:00 2001 From: Nils Hanff Date: Sat, 9 Mar 2024 12:38:06 +0100 Subject: [PATCH 4/4] Change type for translations in element-desktop --- src/language-helper.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/language-helper.ts b/src/language-helper.ts index 26cc19a4c..550d9f69e 100644 --- a/src/language-helper.ts +++ b/src/language-helper.ts @@ -113,7 +113,7 @@ export class AppLocalization { return parts.join("_"); } - public fetchTranslationJson(locale: string): Record | null { + public fetchTranslationJson(locale: string): object | null { try { console.log(`Fetching translation json for locale: ${locale}`); return loadJsonFile(__dirname, "i18n", "strings", `${locale}.json`); @@ -130,13 +130,16 @@ export class AppLocalization { locales = [locales]; } - const loadedLocales = locales.flatMap(this.variations).map(this.denormalize).filter((locale) => { - const translations = this.fetchTranslationJson(locale); - if (translations !== null) { - counterpart.registerTranslations(locale, translations); - } - return !!translations; - }); + const loadedLocales = locales + .flatMap(this.variations) + .map(this.denormalize) + .filter((locale) => { + const translations = this.fetchTranslationJson(locale); + if (translations !== null) { + counterpart.registerTranslations(locale, translations); + } + return !!translations; + }); counterpart.setLocale(loadedLocales[0]); this.store.set(AppLocalization.STORE_KEY, locales);