Skip to content

Commit

Permalink
Use placeholders in misc translations
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner committed Nov 18, 2023
1 parent 1628439 commit 82572d2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 42 deletions.
34 changes: 11 additions & 23 deletions custom_components/sun2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
DOMAIN,
SUNSET_ELEV,
)
from .helpers import LOC_PARAMS, Sun2Data
from .helpers import LOC_PARAMS, Sun2Data, translation
from .sensor import val_tae_cfg, ELEVATION_AT_TIME_SCHEMA, TIME_AT_ELEVATION_SCHEMA

PACKAGE_MERGE_HINT = "list"
Expand Down Expand Up @@ -87,13 +87,6 @@ def _unique_locations_names(configs: list[dict]) -> list[dict]:
)


def _translation(hass: HomeAssistant, key: str) -> str:
"""Sun2 translations."""
return cast(Sun2Data, hass.data[DOMAIN]).translations[
f"component.{DOMAIN}.misc.{key}"
]


def _val_bs_elevation(hass: HomeAssistant, config: str | ConfigType) -> ConfigType:
"""Validate elevation binary_sensor."""
if config[CONF_ELEVATION] == "horizon":
Expand All @@ -102,15 +95,13 @@ def _val_bs_elevation(hass: HomeAssistant, config: str | ConfigType) -> ConfigTy
if config.get(CONF_NAME):
return config

if (elv := config[CONF_ELEVATION]) == DEFAULT_ELEVATION:
name = _translation(hass, "above_horizon")
if (elevation := config[CONF_ELEVATION]) == DEFAULT_ELEVATION:
name = translation(hass, "above_horizon")
else:
above_str = _translation(hass, "above")
if elv < 0:
minus_str = _translation(hass, "minus")
name = f"{above_str} {minus_str} {-elv}"
if elevation < 0:
name = translation(hass, "above_neg_elev", {"elevation": str(-elevation)})
else:
name = f"{above_str} {elv}"
name = translation(hass, "above_pos_elev", {"elevation": str(elevation)})
config[CONF_NAME] = name
return config

Expand All @@ -120,9 +111,9 @@ def _val_eat_name(hass: HomeAssistant, config: str | ConfigType) -> ConfigType:
if config.get(CONF_NAME):
return config

config[
CONF_NAME
] = f"{_translation(hass, 'elevation_at')} {config[CONF_ELEVATION_AT_TIME]}"
config[CONF_NAME] = translation(
hass, "elevation_at", {"elev_time": str(config[CONF_ELEVATION_AT_TIME])}
)

return config

Expand All @@ -135,11 +126,8 @@ def _val_tae_name(hass: HomeAssistant, config: str | ConfigType) -> ConfigType:
direction = SunDirection(config[CONF_DIRECTION])
elevation = cast(float, config[CONF_TIME_AT_ELEVATION])

if elevation >= 0:
elev_str = str(elevation)
else:
elev_str = f"{_translation(hass, 'minus')} {-elevation}"
config[CONF_NAME] = f"{_translation(hass, direction.name.lower())} at {elev_str} °"
trans_key = f"{direction.name.lower()}_{'neg' if elevation < 0 else 'pos'}_elev"
config[CONF_NAME] = translation(hass, trans_key, {"elevation": str(abs(elevation))})

return config

Expand Down
12 changes: 6 additions & 6 deletions custom_components/sun2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.data_entry_flow import FlowResult

from .const import DOMAIN
from .helpers import Sun2Data
from .helpers import Sun2Data, translation


class Sun2ConfigFlow(ConfigFlow, domain=DOMAIN):
Expand All @@ -18,11 +18,11 @@ class Sun2ConfigFlow(ConfigFlow, domain=DOMAIN):

async def async_step_import(self, data: dict[str, Any]) -> FlowResult:
"""Import config entry from configuration."""
location_name = data.pop(CONF_LOCATION, self.hass.config.location_name)
service_name = cast(Sun2Data, self.hass.data[DOMAIN]).translations[
f"component.{DOMAIN}.misc.service_name"
]
title = f"{location_name} {service_name}"
title = translation(
self.hass,
"service_name",
{"location": data.pop(CONF_LOCATION, self.hass.config.location_name)},
)
if existing_entry := await self.async_set_unique_id(data.pop(CONF_UNIQUE_ID)):
self.hass.config_entries.async_update_entry(
existing_entry, title=title, options=data
Expand Down
16 changes: 15 additions & 1 deletion custom_components/sun2/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
CONF_LONGITUDE,
CONF_TIME_ZONE,
)
from homeassistant.core import CALLBACK_TYPE
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType

Expand Down Expand Up @@ -109,6 +109,20 @@ def hours_to_hms(hours: Num | None) -> str | None:
return None


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


_Num = TypeVar("_Num", bound=Num)


Expand Down
14 changes: 8 additions & 6 deletions custom_components/sun2/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"title": "Sun2",
"misc": {
"above": "Above",
"above_horizon": "Above horizon",
"elevation_at": "Elevation at",
"minus": "minus",
"rising": "Rising",
"service_name": "Sun",
"setting": "Setting"
"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} °"
},
"entity": {
"binary_sensor": {
Expand Down
14 changes: 8 additions & 6 deletions custom_components/sun2/translations/nl.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"title": "Zon2",
"misc": {
"above": "Boven",
"above_horizon": "Boven horizon",
"elevation_at": "Hoogte bij",
"minus": "min",
"rising": "Zonsopkomst",
"service_name": "Zon",
"setting": "Zonsondergang"
"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} °"
},
"entity": {
"binary_sensor": {
Expand Down

0 comments on commit 82572d2

Please sign in to comment.