From 043abbb7680b7beff9b3557e12699c39b26e8d17 Mon Sep 17 00:00:00 2001 From: jbouwh Date: Fri, 27 Oct 2023 13:13:55 +0000 Subject: [PATCH 1/6] Add localization to service call exceptions --- src/data/translation.ts | 1 + src/layouts/home-assistant.ts | 3 +++ src/state/connection-mixin.ts | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/src/data/translation.ts b/src/data/translation.ts index bc8165578a99..e4824b0e636f 100644 --- a/src/data/translation.ts +++ b/src/data/translation.ts @@ -61,6 +61,7 @@ export type TranslationCategory = | "state" | "entity" | "entity_component" + | "exceptions" | "config" | "config_panel" | "options" diff --git a/src/layouts/home-assistant.ts b/src/layouts/home-assistant.ts index f9781eef5db5..f955793a3a75 100644 --- a/src/layouts/home-assistant.ts +++ b/src/layouts/home-assistant.ts @@ -173,6 +173,9 @@ export class HomeAssistantAppEl extends QuickBarMixin(HassElement) { // @ts-ignore this._loadHassTranslations(this.hass!.language, "state"); + // @ts-ignore + this._loadHassTranslations(this.hass!.language, "exceptions"); + document.addEventListener( "visibilitychange", () => this._checkVisibility(), diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index 08113c821a2c..b879e0ac8af7 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -122,6 +122,10 @@ export const connectionMixin = >( } if (notifyOnError) { forwardHaptic("failure"); + const lokalizedErrorMessage = (this as any).hass.localize( + `component.${err.translation_domain}.exceptions.${err.translation_key}.message`, + err.translation_placeholders + ); const message = (this as any).hass.localize( "ui.notification_toast.service_call_failed", @@ -129,6 +133,7 @@ export const connectionMixin = >( `${domain}/${service}` ) + ` ${ + lokalizedErrorMessage || err.message || (err.error?.code === ERR_CONNECTION_LOST ? "connection lost" From e8f06e582601b1d0147d0718376f2115ef9f2f17 Mon Sep 17 00:00:00 2001 From: jbouwh Date: Fri, 27 Oct 2023 16:12:54 +0000 Subject: [PATCH 2/6] Only show translated custom error if available --- src/state/connection-mixin.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index b879e0ac8af7..e7e9e8313898 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -127,18 +127,18 @@ export const connectionMixin = >( err.translation_placeholders ); const message = + lokalizedErrorMessage || (this as any).hass.localize( "ui.notification_toast.service_call_failed", "service", `${domain}/${service}` ) + - ` ${ - lokalizedErrorMessage || - err.message || - (err.error?.code === ERR_CONNECTION_LOST - ? "connection lost" - : "unknown error") - }`; + ` ${ + (lokalizedErrorMessage ? "" : err.message) || + (err.error?.code === ERR_CONNECTION_LOST + ? "connection lost" + : "unknown error") + }`; fireEvent(this as any, "hass-notification", { message }); } throw err; From 2366db993339276ba6f99e717e6c1e99cc5c383c Mon Sep 17 00:00:00 2001 From: jbouwh Date: Mon, 30 Oct 2023 13:22:36 +0000 Subject: [PATCH 3/6] Follow up --- src/state/connection-mixin.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index e7e9e8313898..cafc9cf46f04 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -122,19 +122,19 @@ export const connectionMixin = >( } if (notifyOnError) { forwardHaptic("failure"); - const lokalizedErrorMessage = (this as any).hass.localize( + const localizedErrorMessage = (this as any).hass.localize( `component.${err.translation_domain}.exceptions.${err.translation_key}.message`, err.translation_placeholders ); const message = - lokalizedErrorMessage || + localizedErrorMessage || (this as any).hass.localize( "ui.notification_toast.service_call_failed", "service", `${domain}/${service}` ) + ` ${ - (lokalizedErrorMessage ? "" : err.message) || + err.message || (err.error?.code === ERR_CONNECTION_LOST ? "connection lost" : "unknown error") From 876f3a1440778eba42c636a7fffac40e57c26662 Mon Sep 17 00:00:00 2001 From: jbouwh Date: Wed, 1 Nov 2023 17:49:45 +0000 Subject: [PATCH 4/6] Load translations on demand --- src/layouts/home-assistant.ts | 3 --- src/state/connection-mixin.ts | 12 +++++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/layouts/home-assistant.ts b/src/layouts/home-assistant.ts index f955793a3a75..f9781eef5db5 100644 --- a/src/layouts/home-assistant.ts +++ b/src/layouts/home-assistant.ts @@ -173,9 +173,6 @@ export class HomeAssistantAppEl extends QuickBarMixin(HassElement) { // @ts-ignore this._loadHassTranslations(this.hass!.language, "state"); - // @ts-ignore - this._loadHassTranslations(this.hass!.language, "exceptions"); - document.addEventListener( "visibilitychange", () => this._checkVisibility(), diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index cafc9cf46f04..e578f2b230e1 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -122,10 +122,16 @@ export const connectionMixin = >( } if (notifyOnError) { forwardHaptic("failure"); - const localizedErrorMessage = (this as any).hass.localize( - `component.${err.translation_domain}.exceptions.${err.translation_key}.message`, - err.translation_placeholders + const lokalize = await this.hass?.loadBackendTranslation( + "exceptions", + domain ); + const localizedErrorMessage = + lokalize && + lokalize( + `component.${err.translation_domain}.exceptions.${err.translation_key}.message`, + err.translation_placeholders + ); const message = localizedErrorMessage || (this as any).hass.localize( From dada7f7eb43fe371aad8192055eab98c2b99421b Mon Sep 17 00:00:00 2001 From: jbouwh Date: Thu, 2 Nov 2023 22:16:34 +0000 Subject: [PATCH 5/6] Follow up comment --- src/state/connection-mixin.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index e578f2b230e1..01cd4985c754 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -122,16 +122,14 @@ export const connectionMixin = >( } if (notifyOnError) { forwardHaptic("failure"); - const lokalize = await this.hass?.loadBackendTranslation( + const lokalize = await this.hass!.loadBackendTranslation( "exceptions", domain ); - const localizedErrorMessage = - lokalize && - lokalize( - `component.${err.translation_domain}.exceptions.${err.translation_key}.message`, - err.translation_placeholders - ); + const localizedErrorMessage = lokalize( + `component.${err.translation_domain}.exceptions.${err.translation_key}.message`, + err.translation_placeholders + ); const message = localizedErrorMessage || (this as any).hass.localize( From bbce5231ddc02ae3077f1b647057ee9d0d79ca0c Mon Sep 17 00:00:00 2001 From: jbouwh Date: Mon, 6 Nov 2023 14:59:28 +0000 Subject: [PATCH 6/6] Use translation_domain from exception --- src/state/connection-mixin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index 01cd4985c754..8a00bcf1c8ae 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -124,7 +124,7 @@ export const connectionMixin = >( forwardHaptic("failure"); const lokalize = await this.hass!.loadBackendTranslation( "exceptions", - domain + err.translation_domain ); const localizedErrorMessage = lokalize( `component.${err.translation_domain}.exceptions.${err.translation_key}.message`,