Write log on translation error (#20430)

* Write log on translation error

* Update ha-config-devices-dashboard.ts

---------

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Bram Kragten 2024-04-09 13:25:27 +02:00 committed by GitHub
parent 35cf3063cb
commit b091d4f298
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 9 deletions

View File

@ -2,6 +2,7 @@ import IntlMessageFormat from "intl-messageformat";
import type { HTMLTemplateResult } from "lit"; import type { HTMLTemplateResult } from "lit";
import { polyfillLocaleData } from "../../resources/locale-data-polyfill"; import { polyfillLocaleData } from "../../resources/locale-data-polyfill";
import { Resources, TranslationDict } from "../../types"; import { Resources, TranslationDict } from "../../types";
import { fireEvent } from "../dom/fire_event";
// Exclude some patterns from key type checking for now // Exclude some patterns from key type checking for now
// These are intended to be removed as errors are fixed // These are intended to be removed as errors are fixed
@ -81,7 +82,9 @@ export interface FormatsType {
*/ */
export const computeLocalize = async <Keys extends string = LocalizeKeys>( export const computeLocalize = async <Keys extends string = LocalizeKeys>(
cache: any, cache: HTMLElement & {
_localizationCache?: Record<string, IntlMessageFormat>;
},
language: string, language: string,
resources: Resources, resources: Resources,
formats?: FormatsType formats?: FormatsType
@ -107,7 +110,7 @@ export const computeLocalize = async <Keys extends string = LocalizeKeys>(
} }
const messageKey = key + translatedValue; const messageKey = key + translatedValue;
let translatedMessage = cache._localizationCache[messageKey] as let translatedMessage = cache._localizationCache![messageKey] as
| IntlMessageFormat | IntlMessageFormat
| undefined; | undefined;
@ -121,7 +124,7 @@ export const computeLocalize = async <Keys extends string = LocalizeKeys>(
} catch (err: any) { } catch (err: any) {
return "Translation error: " + err.message; return "Translation error: " + err.message;
} }
cache._localizationCache[messageKey] = translatedMessage; cache._localizationCache![messageKey] = translatedMessage;
} }
let argObject = {}; let argObject = {};
@ -137,6 +140,12 @@ export const computeLocalize = async <Keys extends string = LocalizeKeys>(
try { try {
return translatedMessage.format<string>(argObject) as string; return translatedMessage.format<string>(argObject) as string;
} catch (err: any) { } catch (err: any) {
// eslint-disable-next-line no-console
console.error("Translation error", key, language, err);
fireEvent(cache, "write_log", {
level: "error",
message: `Failed to format translation for key '${key}' in language '${language}'. ${err}`,
});
return "Translation " + err; return "Translation " + err;
} }
}; };

View File

@ -496,9 +496,7 @@ export class HaTabsSubpageDataTable extends LitElement {
${this.showFilters && !showPane ${this.showFilters && !showPane
? html`<ha-dialog ? html`<ha-dialog
open open
.heading=${localize("ui.components.subpage-data-table.filters", { .heading=${localize("ui.components.subpage-data-table.filters")}
number: this.data.length,
})}
> >
<ha-dialog-header slot="heading"> <ha-dialog-header slot="heading">
<ha-icon-button <ha-icon-button
@ -510,9 +508,7 @@ export class HaTabsSubpageDataTable extends LitElement {
)} )}
></ha-icon-button> ></ha-icon-button>
<span slot="title" <span slot="title"
>${localize("ui.components.subpage-data-table.filters", { >${localize("ui.components.subpage-data-table.filters")}</span
number: this.data.length,
})}</span
> >
${this.filters ${this.filters
? html`<ha-icon-button ? html`<ha-icon-button