Small cleanups to translation loading (#115583)

- Add missing typing
- Convert a update loop to a set comp
- Save some indent
This commit is contained in:
J. Nick Koston 2024-04-18 09:42:44 -05:00 committed by GitHub
parent 588c260dc5
commit 80d6cdad67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -30,9 +30,11 @@ TRANSLATION_FLATTEN_CACHE = "translation_flatten_cache"
LOCALE_EN = "en" LOCALE_EN = "en"
def recursive_flatten(prefix: Any, data: dict[str, Any]) -> dict[str, Any]: def recursive_flatten(
prefix: str, data: dict[str, dict[str, Any] | str]
) -> dict[str, str]:
"""Return a flattened representation of dict data.""" """Return a flattened representation of dict data."""
output = {} output: dict[str, str] = {}
for key, value in data.items(): for key, value in data.items():
if isinstance(value, dict): if isinstance(value, dict):
output.update(recursive_flatten(f"{prefix}{key}.", value)) output.update(recursive_flatten(f"{prefix}{key}.", value))
@ -250,9 +252,9 @@ class _TranslationCache:
def _validate_placeholders( def _validate_placeholders(
self, self,
language: str, language: str,
updated_resources: dict[str, Any], updated_resources: dict[str, str],
cached_resources: dict[str, Any] | None = None, cached_resources: dict[str, str] | None = None,
) -> dict[str, Any]: ) -> dict[str, str]:
"""Validate if updated resources have same placeholders as cached resources.""" """Validate if updated resources have same placeholders as cached resources."""
if cached_resources is None: if cached_resources is None:
return updated_resources return updated_resources
@ -301,9 +303,11 @@ class _TranslationCache:
"""Extract resources into the cache.""" """Extract resources into the cache."""
resource: dict[str, Any] | str resource: dict[str, Any] | str
cached = self.cache.setdefault(language, {}) cached = self.cache.setdefault(language, {})
categories: set[str] = set() categories = {
for resource in translation_strings.values(): category
categories.update(resource) for component in translation_strings.values()
for category in component
}
for category in categories: for category in categories:
new_resources = build_resources(translation_strings, components, category) new_resources = build_resources(translation_strings, components, category)
@ -312,17 +316,14 @@ class _TranslationCache:
for component, resource in new_resources.items(): for component, resource in new_resources.items():
component_cache = category_cache.setdefault(component, {}) component_cache = category_cache.setdefault(component, {})
if isinstance(resource, dict): if not isinstance(resource, dict):
resources_flatten = recursive_flatten(
f"component.{component}.{category}.",
resource,
)
resources_flatten = self._validate_placeholders(
language, resources_flatten, component_cache
)
component_cache.update(resources_flatten)
else:
component_cache[f"component.{component}.{category}"] = resource component_cache[f"component.{component}.{category}"] = resource
continue
prefix = f"component.{component}.{category}."
flat = recursive_flatten(prefix, resource)
flat = self._validate_placeholders(language, flat, component_cache)
component_cache.update(flat)
@bind_hass @bind_hass