mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Add checks for translation placeholders (#129963)
* Add checks for translation placeholders * Remove async * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review
This commit is contained in:
parent
49bf5db5ff
commit
a3ba7803db
@ -5,6 +5,7 @@ from __future__ import annotations
|
|||||||
from collections.abc import Callable, Generator
|
from collections.abc import Callable, Generator
|
||||||
from importlib.util import find_spec
|
from importlib.util import find_spec
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import string
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
@ -542,17 +543,40 @@ def supervisor_client() -> Generator[AsyncMock]:
|
|||||||
yield supervisor_client
|
yield supervisor_client
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_translation_placeholders(
|
||||||
|
full_key: str,
|
||||||
|
translation: str,
|
||||||
|
description_placeholders: dict[str, str] | None,
|
||||||
|
) -> str | None:
|
||||||
|
"""Raise if translation exists with missing placeholders."""
|
||||||
|
tuples = list(string.Formatter().parse(translation))
|
||||||
|
for _, placeholder, _, _ in tuples:
|
||||||
|
if placeholder is None:
|
||||||
|
continue
|
||||||
|
if (
|
||||||
|
description_placeholders is None
|
||||||
|
or placeholder not in description_placeholders
|
||||||
|
):
|
||||||
|
pytest.fail(
|
||||||
|
f"Description not found for placeholder `{placeholder}` in {full_key}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _ensure_translation_exists(
|
async def _ensure_translation_exists(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
ignore_translations: dict[str, StoreInfo],
|
ignore_translations: dict[str, StoreInfo],
|
||||||
category: str,
|
category: str,
|
||||||
component: str,
|
component: str,
|
||||||
key: str,
|
key: str,
|
||||||
|
description_placeholders: dict[str, str] | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Raise if translation doesn't exist."""
|
"""Raise if translation doesn't exist."""
|
||||||
full_key = f"component.{component}.{category}.{key}"
|
full_key = f"component.{component}.{category}.{key}"
|
||||||
translations = await async_get_translations(hass, "en", category, [component])
|
translations = await async_get_translations(hass, "en", category, [component])
|
||||||
if full_key in translations:
|
if (translation := translations.get(full_key)) is not None:
|
||||||
|
_validate_translation_placeholders(
|
||||||
|
full_key, translation, description_placeholders
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
if full_key in ignore_translations:
|
if full_key in ignore_translations:
|
||||||
@ -610,6 +634,7 @@ def check_config_translations(ignore_translations: str | list[str]) -> Generator
|
|||||||
category,
|
category,
|
||||||
component,
|
component,
|
||||||
f"error.{error}",
|
f"error.{error}",
|
||||||
|
result["description_placeholders"],
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -624,6 +649,7 @@ def check_config_translations(ignore_translations: str | list[str]) -> Generator
|
|||||||
category,
|
category,
|
||||||
component,
|
component,
|
||||||
f"abort.{result["reason"]}",
|
f"abort.{result["reason"]}",
|
||||||
|
result["description_placeholders"],
|
||||||
)
|
)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
Loading…
x
Reference in New Issue
Block a user