Skip to content

Commit

Permalink
Move misc translations to satisfy hassfest
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner committed Nov 29, 2023
1 parent fb9bd2c commit 94210d7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
22 changes: 14 additions & 8 deletions custom_components/sun2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,32 @@ def hours_to_hms(hours: Num | None) -> str | None:
return None


_TRANS_PREFIX = f"component.{DOMAIN}.selector.misc.options"


async def init_translations(hass: HomeAssistant) -> None:
"""Initialize translations."""
data = cast(Sun2Data, hass.data.setdefault(DOMAIN, Sun2Data()))
if data.language != hass.config.language:
data.translations = await async_get_translations(
hass, hass.config.language, "misc", [DOMAIN], False
sel_trans = await async_get_translations(
hass, hass.config.language, "selector", [DOMAIN], False
)
data.translations = {}
for sel_key, val in sel_trans.items():
prefix, key = sel_key.rsplit(".", 1)
if prefix == _TRANS_PREFIX:
data.translations[key] = val


def translate(
hass: HomeAssistant, key: str, placeholders: dict[str, str] | None = None
hass: HomeAssistant, key: str, placeholders: dict[str, Any] | None = None
) -> str:
"""Sun2 translations."""
trans = cast(Sun2Data, hass.data[DOMAIN]).translations[
f"component.{DOMAIN}.misc.{key}"
]
trans = cast(Sun2Data, hass.data[DOMAIN]).translations[key]
if not placeholders:
return trans
for key, val in placeholders.items():
trans = trans.replace(f"{{{key}}}", val)
for ph_key, val in placeholders.items():
trans = trans.replace(f"{{{ph_key}}}", str(val))
return trans


Expand Down
24 changes: 13 additions & 11 deletions custom_components/sun2/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
{
"title": "Sun2",
"misc": {
"above_horizon": "Above horizon",
"above_neg_elev": "Above minus {elevation} °",
"above_pos_elev": "Above {elevation} °",
"elevation_at": "Elevation at {elev_time}",
"rising_neg_elev": "Rising at minus {elevation} °",
"rising_pos_elev": "Rising at {elevation} °",
"service_name": "{location} Sun",
"setting_neg_elev": "Setting at minus {elevation} °",
"setting_pos_elev": "Setting at {elevation} °"
},
"config": {
"step": {
"elevation_at_time_sensor_menu": {
Expand Down Expand Up @@ -437,6 +426,19 @@
"rising": "Rising",
"setting": "Setting"
}
},
"misc": {
"options": {
"above_horizon": "Above horizon",
"above_neg_elev": "Above minus {elevation} °",
"above_pos_elev": "Above {elevation} °",
"elevation_at": "Elevation at {elev_time}",
"rising_neg_elev": "Rising at minus {elevation} °",
"rising_pos_elev": "Rising at {elevation} °",
"service_name": "{location} Sun",
"setting_neg_elev": "Setting at minus {elevation} °",
"setting_pos_elev": "Setting at {elevation} °"
}
}
},
"services": {
Expand Down
24 changes: 13 additions & 11 deletions custom_components/sun2/translations/nl.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
{
"title": "Zon2",
"misc": {
"above_horizon": "Boven horizon",
"above_neg_elev": "Boven min {elevation} °",
"above_pos_elev": "Boven {elevation} °",
"elevation_at": "Hoogte bij {elev_time}",
"rising_neg_elev": "Stijgend bij min {elevation} °",
"rising_pos_elev": "Stijgend bij {elevation} °",
"service_name": "{location} Zon",
"setting_neg_elev": "Instelling bij min {elevation} °",
"setting_pos_elev": "Instelling bij {elevation} °"
},
"config": {
"step": {
"elevation_at_time_sensor_menu": {
Expand Down Expand Up @@ -437,6 +426,19 @@
"rising": "Opstand",
"setting": "Montuur"
}
},
"misc": {
"options": {
"above_horizon": "Boven horizon",
"above_neg_elev": "Boven min {elevation} °",
"above_pos_elev": "Boven {elevation} °",
"elevation_at": "Hoogte bij {elev_time}",
"rising_neg_elev": "Stijgend bij min {elevation} °",
"rising_pos_elev": "Stijgend bij {elevation} °",
"service_name": "{location} Zon",
"setting_neg_elev": "Instelling bij min {elevation} °",
"setting_pos_elev": "Instelling bij {elevation} °"
}
}
},
"services": {
Expand Down

0 comments on commit 94210d7

Please sign in to comment.