Share cached English translation strings across languages (#189368)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Manuel Rüger
2026-11-25 18:03:53 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent c6783cb149
commit 2db0e73b59
2 changed files with 146 additions and 98 deletions
+50 -60
View File
@@ -87,47 +87,33 @@ def build_resources(
async def _async_get_component_strings(
hass: HomeAssistant,
languages: Iterable[str],
language: str,
components: set[str],
integrations: dict[str, Integration],
) -> dict[str, dict[str, Any]]:
) -> dict[str, Any]:
"""Load translations."""
translations_by_language: dict[str, dict[str, Any]] = {}
# Determine paths of missing components/platforms
files_to_load_by_language: dict[str, dict[str, pathlib.Path]] = {}
loaded_translations_by_language: dict[str, dict[str, Any]] = {}
has_files_to_load = False
for language in languages:
file_name = f"{language}.json"
files_to_load: dict[str, pathlib.Path] = {
domain: integration.file_path / "translations" / file_name
for domain in components
if (
(integration := integrations.get(domain))
and integration.has_translations
)
}
files_to_load_by_language[language] = files_to_load
has_files_to_load |= bool(files_to_load)
if has_files_to_load:
loaded_translations_by_language = await hass.async_add_executor_job(
_load_translations_files_by_language, files_to_load_by_language
file_name = f"{language}.json"
files_to_load: dict[str, pathlib.Path] = {
domain: integration.file_path / "translations" / file_name
for domain in components
if ((integration := integrations.get(domain)) and integration.has_translations)
}
loaded_translations: dict[str, Any] = {}
if files_to_load:
loaded_by_language = await hass.async_add_executor_job(
_load_translations_files_by_language, {language: files_to_load}
)
loaded_translations = loaded_by_language.get(language, {})
for language in languages:
loaded_translations = loaded_translations_by_language.setdefault(language, {})
for domain in components:
# Translations that miss "title" will get integration put in.
component_translations = loaded_translations.setdefault(domain, {})
if "title" not in component_translations and (
integration := integrations.get(domain)
):
component_translations["title"] = integration.name
for domain in components:
# Translations that miss "title" will get integration put in.
component_translations = loaded_translations.setdefault(domain, {})
if "title" not in component_translations and (
integration := integrations.get(domain)
):
component_translations["title"] = integration.name
translations_by_language.setdefault(language, {}).update(loaded_translations)
return translations_by_language
return loaded_translations
@dataclass(slots=True)
@@ -228,9 +214,6 @@ class _TranslationCache:
language,
components,
)
# Fetch the English resources, as a fallback for missing keys
languages = [LOCALE_EN] if language == LOCALE_EN else [LOCALE_EN, language]
integrations: dict[str, Integration] = {}
ints_or_excs = await async_get_integrations(self.hass, components)
for domain, int_or_exc in ints_or_excs.items():
@@ -260,8 +243,18 @@ class _TranslationCache:
):
integrations[domain] = integration
translation_by_language_strings = await _async_get_component_strings(
self.hass, languages, components, integrations
# English is the fallback for missing keys. Seed the requested
# language from the English cache so both share the same strings.
english_to_load: set[str] = set()
english_strings: dict[str, Any] = {}
if language != LOCALE_EN and (
english_to_load := components - loaded.setdefault(LOCALE_EN, set())
):
english_strings = await _async_get_component_strings(
self.hass, LOCALE_EN, english_to_load, integrations
)
translation_strings = await _async_get_component_strings(
self.hass, language, components, integrations
)
# An invalidation during I/O must also discard the stale load.
@@ -271,30 +264,27 @@ class _TranslationCache:
if self.generations.get(component, 0) == generations[component]
}
# English is always the fallback language so we load them first
self._build_category_cache(
language, components, translation_by_language_strings[LOCALE_EN]
)
if language != LOCALE_EN:
# Now overlay the requested language on top of the English
self._build_category_cache(
language, components, translation_by_language_strings[language]
)
loaded_english_components = loaded.setdefault(LOCALE_EN, set())
# English is the fallback for missing keys, so cache it for every
# not-yet-cached component, not only when the whole batch is new.
if english_to_cache := components - loaded_english_components:
self._build_category_cache(
LOCALE_EN,
english_to_cache,
translation_by_language_strings[LOCALE_EN],
)
loaded_english_components.update(english_to_cache)
english_to_load &= components
self._build_category_cache(LOCALE_EN, english_to_load, english_strings)
loaded[LOCALE_EN].update(english_to_load)
self._seed_from_english(language, components)
self._build_category_cache(language, components, translation_strings)
loaded[language].update(components)
@callback
def _seed_from_english(self, language: str, components: set[str]) -> None:
"""Seed the cache of a language with the cached English resources."""
cached = self.cache_data.cache.setdefault(language, {})
english = self.cache_data.cache[LOCALE_EN]
for category, english_category_cache in english.items():
category_cache = cached.setdefault(category, {})
for component in components.intersection(english_category_cache):
category_cache.setdefault(component, {}).update(
english_category_cache[component]
)
def _validate_placeholders(
self,
language: str,
+96 -38
View File
@@ -1,6 +1,8 @@
"""Test the translation helper."""
import asyncio
from collections.abc import Generator
from contextlib import contextmanager
import pathlib
from typing import Any
from unittest.mock import Mock, call, patch
@@ -190,7 +192,7 @@ async def test_malformed_localized_placeholders(
}
with patch(
"homeassistant.helpers.translation._async_get_component_strings",
return_value=resources,
side_effect=lambda hass, language, *args: resources[language],
):
result = await translation.async_get_translations(
hass, "de", "entity", ["test"]
@@ -233,7 +235,7 @@ async def test_malformed_english_placeholders(
}
with patch(
"homeassistant.helpers.translation._async_get_component_strings",
return_value=resources,
side_effect=lambda hass, language, *args: resources[language],
):
result = await translation.async_get_translations(
hass, language, "entity", ["test", "other"]
@@ -892,30 +894,24 @@ async def test_get_translations_still_has_title_without_translations_files(
}
async def test_english_cache_populated_for_partial_batch_overlap(
hass: HomeAssistant,
) -> None:
"""Test English caching when a non-English batch partially overlaps it."""
integration_a = Mock(file_path=pathlib.Path(__file__))
integration_a.name = "Component A"
integration_b = Mock(file_path=pathlib.Path(__file__))
integration_b.name = "Component B"
@contextmanager
def _patch_translation_files(
component_strings: dict[str, dict[str, dict[str, Any]]],
) -> Generator[list[set[str]]]:
"""Patch loading translation files and record the languages loaded.
component_strings = {
"comp_a": {
"en": {"issues": {"broken": {"title": "A is broken"}}},
"de": {"issues": {"broken": {"title": "A kaputt"}}},
},
"comp_b": {
"en": {"issues": {"detached": {"title": "B detached"}}},
"de": {"issues": {"detached": {"title": "B abgetrennt"}}},
},
}
component_strings maps component to language to translation strings.
"""
integrations: dict[str, Mock] = {}
for component in component_strings:
integrations[component] = Mock(file_path=pathlib.Path(__file__))
integrations[component].name = component
loaded_languages: list[set[str]] = []
def mock_load_translation_files(
files: dict[str, dict[str, Any]],
) -> dict[str, dict[str, Any]]:
"""Return language-keyed strings for the requested components."""
loaded_languages.append(set(files))
return {
language: {
component: component_strings[component][language]
@@ -924,8 +920,6 @@ async def test_english_cache_populated_for_partial_batch_overlap(
for language, components in files.items()
}
cache = translation._async_get_translations_cache(hass)
with (
patch(
"homeassistant.helpers.translation._load_translations_files_by_language",
@@ -933,8 +927,29 @@ async def test_english_cache_populated_for_partial_batch_overlap(
),
patch(
"homeassistant.helpers.translation.async_get_integrations",
return_value={"comp_a": integration_a, "comp_b": integration_b},
return_value=integrations,
),
):
yield loaded_languages
async def test_english_cache_populated_for_partial_batch_overlap(
hass: HomeAssistant,
) -> None:
"""Test English caching when a non-English batch partially overlaps it."""
cache = translation._async_get_translations_cache(hass)
with _patch_translation_files(
{
"comp_a": {
"en": {"issues": {"broken": {"title": "A is broken"}}},
"de": {"issues": {"broken": {"title": "A kaputt"}}},
},
"comp_b": {
"en": {"issues": {"detached": {"title": "B detached"}}},
"de": {"issues": {"detached": {"title": "B abgetrennt"}}},
},
}
):
# Bootstrap: comp_a is set up (and cached in English) first.
await cache.async_load("en", {"comp_a"})
@@ -995,35 +1010,43 @@ async def test_invalidate_translations_during_load(hass: HomeAssistant) -> None:
"""An older load cannot restore invalidated strings or English fallbacks."""
started = asyncio.Event()
resume = asyncio.Event()
strings = {
"en": {"sensor": {"title": "Old"}, "light": {"title": "Light"}},
"de": {"sensor": {"title": "Alt"}, "light": {"title": "Licht"}},
}
async def load_stale(*args: Any) -> dict[str, dict[str, Any]]:
started.set()
await resume.wait()
return {
"en": {"sensor": {"title": "Old"}, "light": {"title": "Light"}},
"de": {"sensor": {"title": "Alt"}, "light": {"title": "Licht"}},
}
async def load_strings(
hass: HomeAssistant,
language: str,
components: set[str],
integrations: dict[str, Any],
) -> dict[str, Any]:
# Read before blocking, so the first load returns the stale strings.
result = {component: strings[language][component] for component in components}
if not started.is_set():
started.set()
await resume.wait()
return result
with patch(
"homeassistant.helpers.translation._async_get_component_strings",
side_effect=load_stale,
side_effect=load_strings,
) as load:
task = hass.async_create_task(
translation.async_get_translations(hass, "de", "title", {"sensor", "light"})
)
await started.wait()
translation.async_invalidate_translations(hass, {"sensor"})
load.side_effect = None
load.return_value = {
"en": {"sensor": {"title": "New"}},
"de": {"sensor": {"title": "Neu"}},
}
strings["en"]["sensor"] = {"title": "New"}
strings["de"]["sensor"] = {"title": "Neu"}
resume.set()
assert await task == {
"component.sensor.title": "Neu",
"component.light.title": "Licht",
}
assert load.await_count == 2
# English and German are loaded twice: once for the stale and
# once for the reloaded components.
assert load.await_count == 4
assert load.call_args.args[2] == {"sensor"}
assert translation.async_get_cached_translations(hass, "en", "title", "sensor") == {
@@ -1069,3 +1092,38 @@ async def test_invalidate_translations_after_custom_integration_removal(
== expected
)
assert await loader.async_get_integration(hass, domain) is custom
async def test_non_english_reuses_english_cache(hass: HomeAssistant) -> None:
"""Test a non-English load shares the cached English fallback strings."""
cache = translation._async_get_translations_cache(hass)
with _patch_translation_files(
{
"comp_a": {
"en": {
"issues": {
"broken": {"title": "A is broken"},
"detached": {"title": "A detached"},
}
},
"de": {"issues": {"broken": {"title": "A kaputt"}}},
}
}
) as loaded_languages:
await cache.async_load("en", {"comp_a"})
await cache.async_load("de", {"comp_a"})
# English was already cached, so only the German file is read again.
assert loaded_languages == [{"en"}, {"de"}]
english = translation.async_get_cached_translations(hass, "en", "issues", "comp_a")
german = translation.async_get_cached_translations(hass, "de", "issues", "comp_a")
assert german == {
"component.comp_a.issues.broken.title": "A kaputt",
"component.comp_a.issues.detached.title": "A detached",
}
# The English fallback key and value strings are shared, not duplicated.
key = "component.comp_a.issues.detached.title"
assert next(k for k in german if k == key) is next(k for k in english if k == key)
assert german[key] is english[key]