Add localization to service call exceptions (#18447)

This commit is contained in:
Jan Bouwhuis 2023-11-07 12:04:53 +01:00 committed by GitHub
parent 8bdbe8c6a6
commit fd9c24d05e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -61,6 +61,7 @@ export type TranslationCategory =
| "state"
| "entity"
| "entity_component"
| "exceptions"
| "config"
| "config_panel"
| "options"

View File

@ -122,18 +122,27 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
}
if (notifyOnError) {
forwardHaptic("failure");
const lokalize = await this.hass!.loadBackendTranslation(
"exceptions",
err.translation_domain
);
const localizedErrorMessage = lokalize(
`component.${err.translation_domain}.exceptions.${err.translation_key}.message`,
err.translation_placeholders
);
const message =
localizedErrorMessage ||
(this as any).hass.localize(
"ui.notification_toast.service_call_failed",
"service",
`${domain}/${service}`
) +
` ${
err.message ||
(err.error?.code === ERR_CONNECTION_LOST
? "connection lost"
: "unknown error")
}`;
` ${
err.message ||
(err.error?.code === ERR_CONNECTION_LOST
? "connection lost"
: "unknown error")
}`;
fireEvent(this as any, "hass-notification", { message });
}
throw err;