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

View File

@ -2401,7 +2401,10 @@
"zone": "Zone",
"event": "Event",
"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": {
"label": "State",
@ -2448,6 +2451,9 @@
"removed": "removed",
"current": "current",
"updated": "updated"
},
"description": {
"full": "When a persistent notification is updated"
}
},
"sun": {
@ -2519,7 +2525,10 @@
"zone": "[%key:ui::panel::config::automation::editor::triggers::type::zone::label%]",
"event": "[%key:ui::panel::config::automation::editor::triggers::type::homeassistant::event%]",
"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}"
}
}
}
},