Skip to content

Commit e919888

Browse files
jbouwhcoderabbitai[bot]MartinHjelmare
authored
Introducing description placeholders for service action translations (#2844)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Martin Hjelmare <[email protected]>
1 parent 4e8ea9d commit e919888

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
author: Jan Bouwhuis
3+
authorURL: https://github.com/jbouwh
4+
authorImageURL: https://avatars.githubusercontent.com/u/7188918?s=96&v=4
5+
title: Introducing description placeholders for service action translations
6+
---
7+
It is now possible to use translation placeholders for (custom) service actions.
8+
9+
The [service action example](/docs/core/integration-quality-scale/rules/action-setup?_highlight=hass.services.async_register#example-implementation) now shows how to supply the available description placeholders during the registration of the service action.
10+
11+
Move URLs from service descriptions and translation strings into description placeholders.

docs/core/integration-quality-scale/rules/action-setup.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ The example below is a snippet where the service action is registered in the `as
2222
In this example, the service call requires a configuration entry id as parameter.
2323
This is used to first fetch the configuration entry, and then check if it is loaded.
2424
If the configuration entry does not exist or the configuration entry that we found is not loaded, we raise a relevant error which is shown to the user.
25+
Supply description placeholders to enable translation of service parameters, for example, to reference external resources like documentation URLs that need to be localized or updated independently of the service description.
2526

2627
`__init__py`:
27-
```python {13-19} showLineNumbers
28+
```python {13-20} showLineNumbers
2829
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
2930
"""Set up my integration."""
3031

@@ -43,6 +44,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
4344
async_get_schedule,
4445
schema=SERVICE_GET_SCHEDULE_SCHEMA,
4546
supports_response=SupportsResponse.ONLY,
47+
description_placeholders={"example_url": "https://schedule.example.com"}
4648
)
4749
```
4850

docs/dev_101_services.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,18 @@ set_speed:
143143
```
144144
145145
:::info
146-
The name and description of the service actions are set in our [translations](/docs/internationalization/core#services) and not in the service action description. Each service action and service action field must have a matching translation defined.
146+
The name and description of the service actions are set in our [translations](/docs/internationalization/core#services) and not in the service action description. Each service action and service action field must have a matching translation defined. Description placeholders allow you to exclude elements like URLs from translations.
147+
148+
```python
149+
...
150+
hass.services.async_register(
151+
DOMAIN,
152+
"hello", handle_hello,
153+
description_placeholders={"docs_url": "https://example.com/hello_world"},
154+
)
155+
...
156+
```
157+
147158
:::
148159

149160
### Grouping of service action fields

docs/internationalization/core.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ each collapsible section of fields.
183183
Note that also the translations for `name` and `description` of fields which
184184
are displayed in a collapsible section should be under the `fields` key.
185185

186+
Set description placeholders when the [service action is registered](/docs/dev_101_services/#service-action-description-example).
187+
186188
```json
187189
{
188190
"selector": {
@@ -198,7 +200,7 @@ are displayed in a collapsible section should be under the `fields` key.
198200
"services": {
199201
"set_speed": {
200202
"name": "Set speed",
201-
"description": "Sets fan speed.",
203+
"description": "Sets fan speed. [Learn more.]({docs_url})",
202204
"fields": {
203205
"speed": {
204206
"name": "Speed",

0 commit comments

Comments
 (0)