Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit translations for github integration #130538

Merged
merged 9 commits into from
Nov 20, 2024
10 changes: 8 additions & 2 deletions homeassistant/components/youtube/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@
"published_at": { "name": "Published at" }
}
},
"subscribers": { "name": "Subscribers" },
"views": { "name": "Views" }
"subscribers": {
"name": "Subscribers",
"native_unit_of_measurement": "subscribers"
},
"views": {
"name": "Views",
"native_unit_of_measurement": "views"
}
abmantis marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
3 changes: 3 additions & 0 deletions script/hassfest/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
},
slug_validator=translation_key_validator,
),
vol.Optional(
"native_unit_of_measurement"
abmantis marked this conversation as resolved.
Show resolved Hide resolved
): translation_value_validator,
},
slug_validator=translation_key_validator,
),
Expand Down
38 changes: 34 additions & 4 deletions tests/helpers/test_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ def test_load_translations_files_by_language(
"test": {
"entity": {
"switch": {
"other1": {"name": "Other 1"},
"other1": {
"name": "Other 1",
"native_unit_of_measurement": "Units",
abmantis marked this conversation as resolved.
Show resolved Hide resolved
},
"other2": {"name": "Other 2"},
"other3": {"name": "Other 3"},
"other4": {"name": "Other 4"},
"other4": {
"name": "Other 4",
"native_unit_of_measurement": "Quantities",
},
"outlet": {"name": "Outlet " "{placeholder}"},
}
},
Expand All @@ -87,9 +93,11 @@ def test_load_translations_files_by_language(
"en",
{
"component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Units",
"component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}",
},
[],
Expand All @@ -98,9 +106,11 @@ def test_load_translations_files_by_language(
"es",
{
"component.test.entity.switch.other1.name": "Otra 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Units",
"component.test.entity.switch.other2.name": "Otra 2",
"component.test.entity.switch.other3.name": "Otra 3",
"component.test.entity.switch.other4.name": "Otra 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
"component.test.entity.switch.outlet.name": "Enchufe {placeholder}",
},
[],
Expand All @@ -110,12 +120,14 @@ def test_load_translations_files_by_language(
{
# Correct
"component.test.entity.switch.other1.name": "Anderes 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Einheiten",
# Translation has placeholder missing in English
"component.test.entity.switch.other2.name": "Other 2",
# Correct (empty translation)
"component.test.entity.switch.other3.name": "",
# Translation missing
"component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
# Mismatch in placeholders
"component.test.entity.switch.outlet.name": "Outlet {placeholder}",
},
Expand Down Expand Up @@ -166,34 +178,45 @@ async def test_get_translations(hass: HomeAssistant, mock_config_flows) -> None:

assert translations == {
"component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Units",
"component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}",
}

translations = await translation.async_get_translations(
hass, "de", "entity", {"test"}
)

# Test a partial translation
assert translations == {
# Correct
"component.test.entity.switch.other1.name": "Anderes 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Einheiten",
# Translation has placeholder missing in English
"component.test.entity.switch.other2.name": "Other 2",
# Correct (empty translation)
"component.test.entity.switch.other3.name": "",
# Translation missing
"component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
# Mismatch in placeholders
"component.test.entity.switch.outlet.name": "Outlet {placeholder}",
}

# Test a partial translation
translations = await translation.async_get_translations(
hass, "es", "entity", {"test"}
)

assert translations == {
"component.test.entity.switch.other1.name": "Otra 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Units",
"component.test.entity.switch.other2.name": "Otra 2",
"component.test.entity.switch.other3.name": "Otra 3",
"component.test.entity.switch.other4.name": "Otra 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
"component.test.entity.switch.outlet.name": "Enchufe {placeholder}",
}

Expand All @@ -204,9 +227,11 @@ async def test_get_translations(hass: HomeAssistant, mock_config_flows) -> None:

assert translations == {
"component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Units",
"component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}",
}

Expand Down Expand Up @@ -507,9 +532,11 @@ async def test_get_cached_translations(hass: HomeAssistant, mock_config_flows) -
)
assert translations == {
"component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Units",
"component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}",
}

Expand All @@ -522,9 +549,11 @@ async def test_get_cached_translations(hass: HomeAssistant, mock_config_flows) -

assert translations == {
"component.test.entity.switch.other1.name": "Otra 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Units",
"component.test.entity.switch.other2.name": "Otra 2",
"component.test.entity.switch.other3.name": "Otra 3",
"component.test.entity.switch.other4.name": "Otra 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
"component.test.entity.switch.outlet.name": "Enchufe {placeholder}",
}

Expand All @@ -539,9 +568,11 @@ async def test_get_cached_translations(hass: HomeAssistant, mock_config_flows) -

assert translations == {
"component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.native_unit_of_measurement": "Units",
"component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.native_unit_of_measurement": "Quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}",
}

Expand Down Expand Up @@ -678,7 +709,6 @@ async def test_get_translations_still_has_title_without_translations_files(
)

assert translations == translations_again

assert translations == {
"component.component1.title": "Component 1",
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"entity": {
"switch": {
"other1": { "name": "Anderes 1" },
"other1": {
"name": "Anderes 1",
"native_unit_of_measurement": "Einheiten"
},
"other2": { "name": "Anderes 2 {placeholder}" },
"other3": { "name": "" },
"outlet": { "name": "Steckdose {something}" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"entity": {
"switch": {
"other1": { "name": "Other 1" },
"other1": { "name": "Other 1", "native_unit_of_measurement": "Units" },
"other2": { "name": "Other 2" },
"other3": { "name": "Other 3" },
"other4": { "name": "Other 4" },
"other4": {
"name": "Other 4",
"native_unit_of_measurement": "Quantities"
},
"outlet": { "name": "Outlet {placeholder}" }
}
},
Expand Down
Loading