Make some triggers translatable (#17692)

This commit is contained in:
Simon Lamon 2023-09-25 14:07:31 +02:00 committed by GitHub
parent 61982bcb77
commit 071d078e84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 38 deletions

View File

@ -19,6 +19,10 @@ import {
} from "./device_automation"; } from "./device_automation";
import { EntityRegistryEntry } from "./entity_registry"; import { EntityRegistryEntry } from "./entity_registry";
import { FrontendLocaleData } from "./translation"; import { FrontendLocaleData } from "./translation";
import {
formatListWithAnds,
formatListWithOrs,
} from "../common/string/format-list";
const triggerTranslationBaseKey = const triggerTranslationBaseKey =
"ui.panel.config.automation.editor.triggers.type"; "ui.panel.config.automation.editor.triggers.type";
@ -104,11 +108,6 @@ const tryDescribeTrigger = (
return trigger.alias; return trigger.alias;
} }
const disjunctionFormatter = new Intl.ListFormat("en", {
style: "long",
type: "disjunction",
});
// Event Trigger // Event Trigger
if (trigger.platform === "event" && trigger.event_type) { if (trigger.platform === "event" && trigger.event_type) {
const eventTypes: string[] = []; const eventTypes: string[] = [];
@ -121,7 +120,7 @@ const tryDescribeTrigger = (
eventTypes.push(trigger.event_type); eventTypes.push(trigger.event_type);
} }
const eventTypesString = disjunctionFormatter.format(eventTypes); const eventTypesString = formatListWithOrs(hass.locale, eventTypes);
return hass.localize( return hass.localize(
`${triggerTranslationBaseKey}.event.description.full`, `${triggerTranslationBaseKey}.event.description.full`,
{ eventTypes: eventTypesString } { eventTypes: eventTypesString }
@ -242,7 +241,7 @@ const tryDescribeTrigger = (
); );
} }
if (from.length !== 0) { if (from.length !== 0) {
const fromString = disjunctionFormatter.format(from); const fromString = formatListWithOrs(hass.locale, from);
base += ` from ${fromString}`; base += ` from ${fromString}`;
} }
} else { } else {
@ -283,7 +282,7 @@ const tryDescribeTrigger = (
); );
} }
if (to.length !== 0) { if (to.length !== 0) {
const toString = disjunctionFormatter.format(to); const toString = formatListWithOrs(hass.locale, to);
base += ` to ${toString}`; base += ` to ${toString}`;
} }
} else { } else {
@ -356,7 +355,7 @@ const tryDescribeTrigger = (
); );
return hass.localize(`${triggerTranslationBaseKey}.time.description.full`, { return hass.localize(`${triggerTranslationBaseKey}.time.description.full`, {
time: disjunctionFormatter.format(result), time: formatListWithOrs(hass.locale, result),
}); });
} }
@ -505,11 +504,12 @@ const tryDescribeTrigger = (
); );
} }
const entitiesString = disjunctionFormatter.format(entities); return hass.localize(`${triggerTranslationBaseKey}.zone.description.full`, {
const zonesString = disjunctionFormatter.format(zones); entity: formatListWithOrs(hass.locale, entities),
return `When ${entitiesString} ${trigger.event}s ${zonesString} ${ event: trigger.event.toString(),
zones.length > 1 ? "zones" : "zone" zone: formatListWithOrs(hass.locale, zones),
}`; numberOfZones: zones.length,
});
} }
// Geo Location Trigger // Geo Location Trigger
@ -540,11 +540,15 @@ const tryDescribeTrigger = (
); );
} }
const sourcesString = disjunctionFormatter.format(sources); return hass.localize(
const zonesString = disjunctionFormatter.format(zones); `${triggerTranslationBaseKey}.geo_location.description.full`,
return `When ${sourcesString} ${trigger.event}s ${zonesString} ${ {
zones.length > 1 ? "zones" : "zone" source: formatListWithOrs(hass.locale, sources),
}`; event: trigger.event.toString(),
zone: formatListWithOrs(hass.locale, zones),
numberOfZones: zones.length,
}
);
} }
// MQTT Trigger // MQTT Trigger
@ -583,7 +587,8 @@ const tryDescribeTrigger = (
return hass.localize( return hass.localize(
`${triggerTranslationBaseKey}.conversation.description.full`, `${triggerTranslationBaseKey}.conversation.description.full`,
{ {
sentence: disjunctionFormatter.format( sentence: formatListWithOrs(
hass.locale,
ensureArray(trigger.command).map((cmd) => `'${cmd}'`) ensureArray(trigger.command).map((cmd) => `'${cmd}'`)
), ),
} }
@ -592,7 +597,9 @@ const tryDescribeTrigger = (
// Persistent Notification Trigger // Persistent Notification Trigger
if (trigger.platform === "persistent_notification") { if (trigger.platform === "persistent_notification") {
return "When a persistent notification is updated"; return hass.localize(
`${triggerTranslationBaseKey}.persistent_notification.description.full`
);
} }
// Device Trigger // Device Trigger
@ -650,15 +657,6 @@ const tryDescribeCondition = (
return condition.alias; return condition.alias;
} }
const conjunctionFormatter = new Intl.ListFormat("en", {
style: "long",
type: "conjunction",
});
const disjunctionFormatter = new Intl.ListFormat("en", {
style: "long",
type: "disjunction",
});
if (!condition.condition) { if (!condition.condition) {
const shorthands: Array<"and" | "or" | "not"> = ["and", "or", "not"]; const shorthands: Array<"and" | "or" | "not"> = ["and", "or", "not"];
for (const key of shorthands) { for (const key of shorthands) {
@ -756,8 +754,8 @@ const tryDescribeCondition = (
if (entities.length !== 0) { if (entities.length !== 0) {
const entitiesString = const entitiesString =
condition.match === "any" condition.match === "any"
? disjunctionFormatter.format(entities) ? formatListWithOrs(hass.locale, entities)
: conjunctionFormatter.format(entities); : formatListWithAnds(hass.locale, entities);
base += ` ${entitiesString} ${ base += ` ${entitiesString} ${
condition.entity_id.length > 1 ? "are" : "is" condition.entity_id.length > 1 ? "are" : "is"
}`; }`;
@ -812,7 +810,7 @@ const tryDescribeCondition = (
states.push("a state"); states.push("a state");
} }
const statesString = disjunctionFormatter.format(states); const statesString = formatListWithOrs(hass.locale, states);
base += ` ${statesString}`; base += ` ${statesString}`;
if (condition.for) { if (condition.for) {
@ -902,7 +900,7 @@ const tryDescribeCondition = (
`ui.panel.config.automation.editor.conditions.type.time.weekdays.${d}` `ui.panel.config.automation.editor.conditions.type.time.weekdays.${d}`
) )
); );
result += " day is " + disjunctionFormatter.format(localizedDays); result += " day is " + formatListWithOrs(hass.locale, localizedDays);
} }
return result; return result;
@ -981,8 +979,8 @@ const tryDescribeCondition = (
); );
} }
const entitiesString = disjunctionFormatter.format(entities); const entitiesString = formatListWithOrs(hass.locale, entities);
const zonesString = disjunctionFormatter.format(zones); const zonesString = formatListWithOrs(hass.locale, zones);
return hass.localize( return hass.localize(
`${conditionsTranslationBaseKey}.zone.description.full`, `${conditionsTranslationBaseKey}.zone.description.full`,
{ {

View File

@ -2401,7 +2401,10 @@
"zone": "Zone", "zone": "Zone",
"event": "Event", "event": "Event",
"enter": "Enter", "enter": "Enter",
"leave": "Leave" "leave": "Leave",
"description": {
"full": "When {source} {event, select, \n enter {enters}\n leave {leaves} other {} \n} {zone} {numberOfZones, plural,\n one {zone}\n other {zones}\n}"
}
}, },
"state": { "state": {
"label": "State", "label": "State",
@ -2448,6 +2451,9 @@
"removed": "removed", "removed": "removed",
"current": "current", "current": "current",
"updated": "updated" "updated": "updated"
},
"description": {
"full": "When a persistent notification is updated"
} }
}, },
"sun": { "sun": {
@ -2519,7 +2525,10 @@
"zone": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]", "zone": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]",
"event": "[%key:ui::panel::config::automation::editor::triggers::type::homeassistant::event%]", "event": "[%key:ui::panel::config::automation::editor::triggers::type::homeassistant::event%]",
"enter": "Enter", "enter": "Enter",
"leave": "Leave" "leave": "Leave",
"description": {
"full": "When {entity} {event, select, \n enter {enters}\n leave {leaves} other {} \n} {zone} {numberOfZones, plural,\n one {zone} \n other {zones}\n}"
}
} }
} }
}, },