diff --git a/custom_components/sun2/helpers.py b/custom_components/sun2/helpers.py index b41b930..86be9e9 100644 --- a/custom_components/sun2/helpers.py +++ b/custom_components/sun2/helpers.py @@ -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 diff --git a/custom_components/sun2/translations/en.json b/custom_components/sun2/translations/en.json index a6d0276..b0488fd 100644 --- a/custom_components/sun2/translations/en.json +++ b/custom_components/sun2/translations/en.json @@ -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": { @@ -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": { diff --git a/custom_components/sun2/translations/nl.json b/custom_components/sun2/translations/nl.json index 18b4c66..0ee0570 100644 --- a/custom_components/sun2/translations/nl.json +++ b/custom_components/sun2/translations/nl.json @@ -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": { @@ -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": {