mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 19:26:36 +00:00
Localize time string in time trigger and condition (#16681)
This commit is contained in:
parent
a9f1c4a198
commit
748925ede9
@ -1,4 +1,9 @@
|
|||||||
import { formatDuration } from "../common/datetime/format_duration";
|
import { formatDuration } from "../common/datetime/format_duration";
|
||||||
|
import {
|
||||||
|
formatTime,
|
||||||
|
formatTimeWithSeconds,
|
||||||
|
} from "../common/datetime/format_time";
|
||||||
|
import { FrontendLocaleData } from "./translation";
|
||||||
import secondsToDuration from "../common/datetime/seconds_to_duration";
|
import secondsToDuration from "../common/datetime/seconds_to_duration";
|
||||||
import { ensureArray } from "../common/array/ensure-array";
|
import { ensureArray } from "../common/array/ensure-array";
|
||||||
import { computeStateName } from "../common/entity/compute_state_name";
|
import { computeStateName } from "../common/entity/compute_state_name";
|
||||||
@ -29,6 +34,22 @@ const describeDuration = (forTime: number | string | ForDict) => {
|
|||||||
return duration;
|
return duration;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const localizeTimeString = (time: string, locale: FrontendLocaleData) => {
|
||||||
|
const chunks = time.split(":");
|
||||||
|
if (chunks.length < 2 || chunks.length > 3) {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const dt = new Date("1970-01-01T" + time);
|
||||||
|
if (chunks.length === 2 || Number(chunks[2]) === 0) {
|
||||||
|
return formatTime(dt, locale);
|
||||||
|
}
|
||||||
|
return formatTimeWithSeconds(dt, locale);
|
||||||
|
} catch {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const ordinalSuffix = (n: number) => {
|
const ordinalSuffix = (n: number) => {
|
||||||
n %= 100;
|
n %= 100;
|
||||||
if ([11, 12, 13].includes(n)) {
|
if ([11, 12, 13].includes(n)) {
|
||||||
@ -320,9 +341,11 @@ export const describeTrigger = (
|
|||||||
// Time Trigger
|
// Time Trigger
|
||||||
if (trigger.platform === "time" && trigger.at) {
|
if (trigger.platform === "time" && trigger.at) {
|
||||||
const result = ensureArray(trigger.at).map((at) =>
|
const result = ensureArray(trigger.at).map((at) =>
|
||||||
at.toString().includes(".")
|
typeof at !== "string"
|
||||||
|
? at
|
||||||
|
: at.includes(".")
|
||||||
? `entity ${hass.states[at] ? computeStateName(hass.states[at]) : at}`
|
? `entity ${hass.states[at] ? computeStateName(hass.states[at]) : at}`
|
||||||
: at
|
: localizeTimeString(at, hass.locale)
|
||||||
);
|
);
|
||||||
|
|
||||||
const last = result.splice(-1, 1)[0];
|
const last = result.splice(-1, 1)[0];
|
||||||
@ -790,21 +813,27 @@ export const describeCondition = (
|
|||||||
const validWeekdays =
|
const validWeekdays =
|
||||||
weekdaysArray && weekdaysArray.length > 0 && weekdaysArray.length < 7;
|
weekdaysArray && weekdaysArray.length > 0 && weekdaysArray.length < 7;
|
||||||
if (condition.before || condition.after || validWeekdays) {
|
if (condition.before || condition.after || validWeekdays) {
|
||||||
const before = condition.before?.toString().includes(".")
|
const before =
|
||||||
? `entity ${
|
typeof condition.before !== "string"
|
||||||
hass.states[condition.before]
|
? condition.before
|
||||||
? computeStateName(hass.states[condition.before])
|
: condition.before.includes(".")
|
||||||
: condition.before
|
? `entity ${
|
||||||
}`
|
hass.states[condition.before]
|
||||||
: condition.before;
|
? computeStateName(hass.states[condition.before])
|
||||||
|
: condition.before
|
||||||
|
}`
|
||||||
|
: localizeTimeString(condition.before, hass.locale);
|
||||||
|
|
||||||
const after = condition.after?.toString().includes(".")
|
const after =
|
||||||
? `entity ${
|
typeof condition.after !== "string"
|
||||||
hass.states[condition.after]
|
? condition.after
|
||||||
? computeStateName(hass.states[condition.after])
|
: condition.after.includes(".")
|
||||||
: condition.after
|
? `entity ${
|
||||||
}`
|
hass.states[condition.after]
|
||||||
: condition.after;
|
? computeStateName(hass.states[condition.after])
|
||||||
|
: condition.after
|
||||||
|
}`
|
||||||
|
: localizeTimeString(condition.after, hass.locale);
|
||||||
|
|
||||||
let result = "Confirm the ";
|
let result = "Confirm the ";
|
||||||
if (after || before) {
|
if (after || before) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user