mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 02:06:42 +00:00
Add support for unit_of_measurement translation (#22868)
This commit is contained in:
parent
a9d7082218
commit
84f7c62ee2
@ -103,7 +103,12 @@ export const computeStateDisplayFromEntityAttributes = (
|
|||||||
getNumberFormatOptions({ state, attributes } as HassEntity, entity)
|
getNumberFormatOptions({ state, attributes } as HassEntity, entity)
|
||||||
);
|
);
|
||||||
|
|
||||||
const unit = attributes.unit_of_measurement;
|
const unit =
|
||||||
|
(entity?.translation_key &&
|
||||||
|
localize(
|
||||||
|
`component.${entity.platform}.entity.${domain}.${entity.translation_key}.unit_of_measurement`
|
||||||
|
)) ||
|
||||||
|
attributes.unit_of_measurement;
|
||||||
|
|
||||||
if (unit) {
|
if (unit) {
|
||||||
return `${value}${blankBeforeUnit(unit, locale)}${unit}`;
|
return `${value}${blankBeforeUnit(unit, locale)}${unit}`;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { assert } from "chai";
|
import { assert } from "chai";
|
||||||
import { computeStateDisplay } from "../../../src/common/entity/compute_state_display";
|
import { computeStateDisplay } from "../../../src/common/entity/compute_state_display";
|
||||||
import { UNKNOWN } from "../../../src/data/entity";
|
import { UNKNOWN } from "../../../src/data/entity";
|
||||||
|
import type { FrontendLocaleData } from "../../../src/data/translation";
|
||||||
import {
|
import {
|
||||||
FrontendLocaleData,
|
|
||||||
NumberFormat,
|
NumberFormat,
|
||||||
TimeFormat,
|
TimeFormat,
|
||||||
FirstWeekday,
|
FirstWeekday,
|
||||||
@ -119,6 +119,33 @@ describe("computeStateDisplay", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Localizes a numeric sensor value with translated unit_of_measurement", () => {
|
||||||
|
const stateObj: any = {
|
||||||
|
entity_id: "sensor.test",
|
||||||
|
state: "1234",
|
||||||
|
attributes: {
|
||||||
|
state_class: "measurement",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const entities: any = {
|
||||||
|
"sensor.test": {
|
||||||
|
translation_key: "custom_translation",
|
||||||
|
platform: "custom_integration",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
assert.strictEqual(
|
||||||
|
computeStateDisplay(
|
||||||
|
localize,
|
||||||
|
stateObj,
|
||||||
|
localeData,
|
||||||
|
numericDeviceClasses,
|
||||||
|
demoConfig,
|
||||||
|
entities
|
||||||
|
),
|
||||||
|
"1,234 component.custom_integration.entity.sensor.custom_translation.unit_of_measurement"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it("Localizes and formats numeric sensor value with units", () => {
|
it("Localizes and formats numeric sensor value with units", () => {
|
||||||
const stateObj: any = {
|
const stateObj: any = {
|
||||||
entity_id: "sensor.test",
|
entity_id: "sensor.test",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user