Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements to service call descriptions. #20733

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/data/script_i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,19 @@ const tryDescribeAction = <T extends ActionType>(
const config = action as ActionTypes["service"];

const targets: string[] = [];
if (config.target) {
const targetOrData = config.target || config.data;
if (targetOrData) {
for (const [key, name] of Object.entries({
area_id: "areas",
device_id: "devices",
entity_id: "entities",
floor_id: "floors",
label_id: "labels",
})) {
if (!(key in config.target)) {
if (!(key in targetOrData)) {
continue;
}
const keyConf: string[] = Array.isArray(config.target[key])
? config.target[key]
: [config.target[key]];
const keyConf: string[] = ensureArray(targetOrData[key]) || [];

for (const targetThing of keyConf) {
if (isTemplate(targetThing)) {
Expand Down Expand Up @@ -196,7 +195,10 @@ const tryDescribeAction = <T extends ActionType>(
) {
return hass.localize(
`${actionTranslationBaseKey}.service.description.service_based_on_template`,
{ targets: formatListWithAnds(hass.locale, targets) }
{
hasTargets: targets.length ? "true" : "false",
targets: formatListWithAnds(hass.locale, targets),
}
);
}

Expand All @@ -212,6 +214,7 @@ const tryDescribeAction = <T extends ActionType>(
{
domain: domainToName(hass.localize, domain),
name: service || config.service,
hasTargets: targets.length ? "true" : "false",
targets: formatListWithAnds(hass.locale, targets),
}
);
Expand All @@ -223,6 +226,7 @@ const tryDescribeAction = <T extends ActionType>(
name: service
? `${domainToName(hass.localize, domain)}: ${service}`
: config.service,
hasTargets: targets.length ? "true" : "false",
targets: formatListWithAnds(hass.locale, targets),
}
);
Expand Down
6 changes: 3 additions & 3 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3248,9 +3248,9 @@
"has_optional_response": "This service can return a response, if you want to use the response, enter the name of a variable the response will be saved in",
"has_response": "This service returns a response, enter the name of a variable the response will be saved in",
"description": {
"service_based_on_template": "Call a service based on a template on {targets}",
"service_based_on_name": "Call a service ''{name}'' on {targets}",
"service_name": "{domain} ''{name}'' on {targets}",
"service_based_on_template": "Call a service based on a template{hasTargets, select, \n true { on {targets}} \n other {} \n }",
"service_based_on_name": "Call a service ''{name}''{hasTargets, select, \n true { on {targets}} \n other {} \n }",
"service_name": "{domain} ''{name}''{hasTargets, select, \n true { on {targets}} \n other {} \n }",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we split each translation in two keys to simplify translator work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've already got dozens of keys using a similar pattern. Should I consider {select} as always discouraged? Having two translations for the same sentence seems cluttery, and this does not seem exceptionally complex, but I will follow guidance if we have some.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've introduced many of these, and the general feedback from those pull requests was to split the sentence when there were multiple selects, nested selects or it became pretty long in number of characters.
If it's really needed, I would suggest to introduce keys for when no target exists, so the existing translations don't need to be translated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed, it saves missing translations (old ones would need to get updated) and makes it a bit easier for translators (although I'm surprised how well it goes actually!)

"service": "Call a service",
"target_template": "templated {name}",
"target_unknown_entity": "unknown entity",
Expand Down
Loading