Support translating number selector UoM (#26070)

This commit is contained in:
karwosts 2025-07-04 11:06:49 -07:00 committed by GitHub
parent 257769cdc7
commit 4d932f0b4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View File

@ -23,6 +23,9 @@ export class HaNumberSelector extends LitElement {
@property() public helper?: string; @property() public helper?: string;
@property({ attribute: false })
public localizeValue?: (key: string) => string;
@property({ type: Boolean }) public required = true; @property({ type: Boolean }) public required = true;
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
@ -60,6 +63,14 @@ export class HaNumberSelector extends LitElement {
} }
} }
const translationKey = this.selector.number?.translation_key;
let unit = this.selector.number?.unit_of_measurement;
if (isBox && unit && this.localizeValue && translationKey) {
unit =
this.localizeValue(`${translationKey}.unit_of_measurement.${unit}`) ||
unit;
}
return html` return html`
${this.label && !isBox ${this.label && !isBox
? html`${this.label}${this.required ? "*" : ""}` ? html`${this.label}${this.required ? "*" : ""}`
@ -97,7 +108,7 @@ export class HaNumberSelector extends LitElement {
.helper=${isBox ? this.helper : undefined} .helper=${isBox ? this.helper : undefined}
.disabled=${this.disabled} .disabled=${this.disabled}
.required=${this.required} .required=${this.required}
.suffix=${this.selector.number?.unit_of_measurement} .suffix=${unit}
type="number" type="number"
autoValidate autoValidate
?no-spinner=${!isBox} ?no-spinner=${!isBox}

View File

@ -333,6 +333,7 @@ export interface NumberSelector {
mode?: "box" | "slider"; mode?: "box" | "slider";
unit_of_measurement?: string; unit_of_measurement?: string;
slider_ticks?: boolean; slider_ticks?: boolean;
translation_key?: string;
} | null; } | null;
} }