From 61075487c361057132b32b6df126d7e5a9b3e849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sun, 27 Nov 2022 15:17:37 +0100 Subject: [PATCH] Allways fallback to default language (#628) --- src/components/dialogs/hacs-dialog-base.ts | 4 +- .../dialogs/hacs-download-dialog.ts | 2 +- src/localize/localize.ts | 42 ++++++++++--------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/components/dialogs/hacs-dialog-base.ts b/src/components/dialogs/hacs-dialog-base.ts index 3791cef1d..e9f109b3a 100644 --- a/src/components/dialogs/hacs-dialog-base.ts +++ b/src/components/dialogs/hacs-dialog-base.ts @@ -24,7 +24,7 @@ export class HacsDialogBase extends LitElement { shouldUpdate(changedProperties: PropertyValues) { changedProperties.forEach((_oldValue, propName) => { if (propName === "hass") { - this.sidebarDocked = window.localStorage.getItem("dockedSidebar") === '"docked"'; + this.sidebarDocked = window?.localStorage?.getItem("dockedSidebar") === '"docked"'; } }); return ( @@ -42,6 +42,6 @@ export class HacsDialogBase extends LitElement { public connectedCallback() { super.connectedCallback(); - this.sidebarDocked = window.localStorage.getItem("dockedSidebar") === '"docked"'; + this.sidebarDocked = window?.localStorage?.getItem("dockedSidebar") === '"docked"'; } } diff --git a/src/components/dialogs/hacs-download-dialog.ts b/src/components/dialogs/hacs-download-dialog.ts index 4b09067f7..be1c333bd 100644 --- a/src/components/dialogs/hacs-download-dialog.ts +++ b/src/components/dialogs/hacs-download-dialog.ts @@ -41,7 +41,7 @@ export class HacsDonwloadDialog extends HacsDialogBase { shouldUpdate(changedProperties: PropertyValues) { changedProperties.forEach((_oldValue, propName) => { if (propName === "hass") { - this.sidebarDocked = window.localStorage.getItem("dockedSidebar") === '"docked"'; + this.sidebarDocked = window?.localStorage?.getItem("dockedSidebar") === '"docked"'; } if (propName === "repositories") { this._fetchRepository(); diff --git a/src/localize/localize.ts b/src/localize/localize.ts index 07e5a3384..e35c87bb1 100644 --- a/src/localize/localize.ts +++ b/src/localize/localize.ts @@ -5,47 +5,47 @@ import { languages } from "./generated"; const DEFAULT_LANGUAGE = "en"; const IGNORE_LANGUAGES = new Set(["en_GB"]); const logger = new HacsLogger("localize"); -const warnings: { language: string[]; sting: Record> } = { - language: [], - sting: {}, -}; const _localizationCache = {}; export function localize(language: string, string: string, replace?: Record): string { - let lang = (language || localStorage.getItem("selectedLanguage") || DEFAULT_LANGUAGE) + try { + return _localize(language, string, replace); + } catch (err: any) { + logger.error(err?.message || `Unknown error translating '${string}' with '${language}'`); + return _localize(DEFAULT_LANGUAGE, string, replace); + } +} + +function _localize(language: string, string: string, replace?: Record): string { + let lang = (language || window?.localStorage?.getItem("selectedLanguage") || DEFAULT_LANGUAGE) .replace(/['"]+/g, "") .replace("-", "_"); - if (IGNORE_LANGUAGES.has(lang) || !languages[lang]) { - if (!IGNORE_LANGUAGES.has(lang) && !warnings.language?.includes(lang)) { - warnings.language.push(lang); - logger.warn( - `Language '${lang.replace( - "_", - "-" - )}' is not added to HACS, using '${DEFAULT_LANGUAGE}' instead. https://hacs.xyz/docs/developer/translation` - ); - } + if (!languages[lang] || IGNORE_LANGUAGES.has(lang)) { lang = DEFAULT_LANGUAGE; } const translatedValue = languages[lang]?.[string] || languages[DEFAULT_LANGUAGE][string]; if (!translatedValue) { - logger.error(`Translation problem with '${string}' for '${lang}'`); + if (lang !== DEFAULT_LANGUAGE) { + throw Error(`'${string}' is unknown'`); + } return string; } - const messageKey = string + translatedValue; + const messageKey = `${string}_${translatedValue}`; let translatedMessage = _localizationCache[messageKey] as IntlMessageFormat | undefined; if (!translatedMessage) { try { - translatedMessage = new IntlMessageFormat(translatedValue, language); + translatedMessage = new IntlMessageFormat(translatedValue, lang); } catch (err: any) { - logger.error(`Translation problem with '${string}' for '${lang}'`); + if (lang !== DEFAULT_LANGUAGE) { + throw Error(`Translation problem with '${string}' for '${lang}'`); + } return string; } _localizationCache[messageKey] = translatedMessage; @@ -54,7 +54,9 @@ export function localize(language: string, string: string, replace?: Record(replace) as string; } catch (err: any) { - logger.error(`Translation problem with '${string}' for '${lang}'`); + if (lang !== DEFAULT_LANGUAGE) { + throw Error(`Translation problem with placeholders for '${string}' for '${lang}'`); + } return string; } }