Move localizing to render (#8419)

This commit is contained in:
Bram Kragten 2021-02-19 18:02:25 +01:00 committed by GitHub
parent a41afcd714
commit 9807d0aede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 28 deletions

View File

@ -127,7 +127,7 @@ class HaPanelMy extends LitElement {
@property() public route!: Route; @property() public route!: Route;
@internalProperty() public _error = ""; @internalProperty() public _error?: string;
connectedCallback() { connectedCallback() {
super.connectedCallback(); super.connectedCallback();
@ -135,11 +135,7 @@ class HaPanelMy extends LitElement {
if (path.startsWith("supervisor")) { if (path.startsWith("supervisor")) {
if (!isComponentLoaded(this.hass, "hassio")) { if (!isComponentLoaded(this.hass, "hassio")) {
this._error = this.hass.localize( this._error = "no_supervisor";
"ui.panel.my.component_not_loaded",
"integration",
domainToName(this.hass.localize, "hassio")
);
return; return;
} }
navigate( navigate(
@ -153,16 +149,7 @@ class HaPanelMy extends LitElement {
const redirect = REDIRECTS[path]; const redirect = REDIRECTS[path];
if (!redirect) { if (!redirect) {
this._error = this.hass.localize( this._error = "not_supported";
"ui.panel.my.not_supported",
"link",
html`<a
target="_blank"
rel="noreferrer noopener"
href="https://my.home-assistant.io/faq.html#supported-pages"
>${this.hass.localize("ui.panel.my.faq_link")}</a
>`
);
return; return;
} }
@ -170,11 +157,7 @@ class HaPanelMy extends LitElement {
redirect.component && redirect.component &&
!isComponentLoaded(this.hass, redirect.component) !isComponentLoaded(this.hass, redirect.component)
) { ) {
this._error = this.hass.localize( this._error = "no_component";
"ui.panel.my.component_not_loaded",
"integration",
domainToName(this.hass.localize, redirect.component)
);
return; return;
} }
@ -182,7 +165,7 @@ class HaPanelMy extends LitElement {
try { try {
url = this._createRedirectUrl(redirect); url = this._createRedirectUrl(redirect);
} catch (err) { } catch (err) {
this._error = this.hass.localize("ui.panel.my.error"); this._error = "url_error";
return; return;
} }
@ -191,9 +174,44 @@ class HaPanelMy extends LitElement {
protected render() { protected render() {
if (this._error) { if (this._error) {
return html`<hass-error-screen let error = "Unknown error";
.error=${this._error} switch (this._error) {
></hass-error-screen>`; case "not_supported":
error =
this.hass.localize(
"ui.panel.my.not_supported",
"link",
html`<a
target="_blank"
rel="noreferrer noopener"
href="https://my.home-assistant.io/faq.html#supported-pages"
>${this.hass.localize("ui.panel.my.faq_link")}</a
>`
) || "This redirect is not supported.";
break;
case "no_component":
error =
this.hass.localize(
"ui.panel.my.component_not_loaded",
"integration",
domainToName(
this.hass.localize,
REDIRECTS[this.route.path.substr(1)].component!
)
) || "This redirect is not supported.";
break;
case "no_supervisor":
error =
this.hass.localize(
"ui.panel.my.component_not_loaded",
"integration",
"Home Assistant Supervisor"
) || "This redirect requires Home Assistant Supervisor.";
break;
default:
error = this.hass.localize("ui.panel.my.error") || "Unknown error";
}
return html`<hass-error-screen .error=${error}></hass-error-screen>`;
} }
return html``; return html``;
} }

View File

@ -51,6 +51,17 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
this._loadCoreTranslations(getLocalLanguage()); this._loadCoreTranslations(getLocalLanguage());
} }
protected updated(changedProps) {
super.updated(changedProps);
if (!changedProps.has("hass")) {
return;
}
const oldHass = changedProps.get("hass");
if (this.hass?.panels && oldHass.panels !== this.hass.panels) {
this._loadFragmentTranslations(this.hass.language, this.hass.panelUrl);
}
}
protected hassConnected() { protected hassConnected() {
super.hassConnected(); super.hassConnected();
getUserLanguage(this.hass!).then((language) => { getUserLanguage(this.hass!).then((language) => {
@ -204,13 +215,10 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
const panelComponent = this.hass?.panels?.[panelUrl]?.component_name; const panelComponent = this.hass?.panels?.[panelUrl]?.component_name;
// If it's the first call we don't have panel info yet to check the component. // If it's the first call we don't have panel info yet to check the component.
// If the url is not known it might be a custom lovelace dashboard, so we load lovelace translations
const fragment = translationMetadata.fragments.includes( const fragment = translationMetadata.fragments.includes(
panelComponent || panelUrl panelComponent || panelUrl
) )
? panelComponent || panelUrl ? panelComponent || panelUrl
: !panelComponent
? "lovelace"
: undefined; : undefined;
if (!fragment) { if (!fragment) {