Replace dash in language if needed (#106559)

* Replace dash in language if needed

* Add tests
This commit is contained in:
G Johansson 2023-12-28 20:16:14 +01:00 committed by Bram Kragten
parent 285bb5632d
commit d24a923a73
2 changed files with 78 additions and 5 deletions

View File

@ -47,7 +47,7 @@ class HolidayConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self._async_abort_entries_match({CONF_COUNTRY: user_input[CONF_COUNTRY]})
try:
locale = Locale(self.hass.config.language)
locale = Locale(self.hass.config.language.replace("-", "_"))
except UnknownLocaleError:
# Default to (US) English if language not recognized by babel
# Mainly an issue with English flavors such as "en-GB"
@ -87,7 +87,7 @@ class HolidayConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
)
try:
locale = Locale(self.hass.config.language)
locale = Locale(self.hass.config.language.replace("-", "_"))
except UnknownLocaleError:
# Default to (US) English if language not recognized by babel
# Mainly an issue with English flavors such as "en-GB"

View File

@ -130,13 +130,13 @@ async def test_single_combination_country_province(hass: HomeAssistant) -> None:
async def test_form_babel_unresolved_language(hass: HomeAssistant) -> None:
"""Test the config flow if using not babel supported language."""
hass.config.language = "en-GB"
hass.config.language = "en-XX"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "SE",
@ -144,4 +144,77 @@ async def test_form_babel_unresolved_language(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
assert result2["title"] == "Sweden"
assert result["title"] == "Sweden"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_PROVINCE: "BW",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Germany, BW"
assert result["data"] == {
"country": "DE",
"province": "BW",
}
async def test_form_babel_replace_dash_with_underscore(hass: HomeAssistant) -> None:
"""Test the config flow if using language with dash."""
hass.config.language = "en-GB"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "SE",
},
)
await hass.async_block_till_done()
assert result["title"] == "Sweden"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_COUNTRY: "DE",
},
)
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_PROVINCE: "BW",
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Germany, BW"
assert result["data"] == {
"country": "DE",
"province": "BW",
}