mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Only download translation strings we have defined (#131864)
This commit is contained in:
parent
889ac1552b
commit
9db6f0ffc4
@ -7,8 +7,7 @@ from pathlib import Path
|
||||
from homeassistant.const import Platform
|
||||
|
||||
from . import upload
|
||||
from .develop import flatten_translations
|
||||
from .util import get_base_arg_parser, load_json_from_path
|
||||
from .util import flatten_translations, get_base_arg_parser, load_json_from_path
|
||||
|
||||
|
||||
def get_arguments() -> argparse.Namespace:
|
||||
|
@ -9,7 +9,7 @@ import sys
|
||||
|
||||
from . import download, upload
|
||||
from .const import INTEGRATIONS_DIR
|
||||
from .util import get_base_arg_parser
|
||||
from .util import flatten_translations, get_base_arg_parser
|
||||
|
||||
|
||||
def valid_integration(integration):
|
||||
@ -32,29 +32,6 @@ def get_arguments() -> argparse.Namespace:
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def flatten_translations(translations):
|
||||
"""Flatten all translations."""
|
||||
stack = [iter(translations.items())]
|
||||
key_stack = []
|
||||
flattened_translations = {}
|
||||
while stack:
|
||||
for k, v in stack[-1]:
|
||||
key_stack.append(k)
|
||||
if isinstance(v, dict):
|
||||
stack.append(iter(v.items()))
|
||||
break
|
||||
if isinstance(v, str):
|
||||
common_key = "::".join(key_stack)
|
||||
flattened_translations[common_key] = v
|
||||
key_stack.pop()
|
||||
else:
|
||||
stack.pop()
|
||||
if key_stack:
|
||||
key_stack.pop()
|
||||
|
||||
return flattened_translations
|
||||
|
||||
|
||||
def substitute_translation_references(integration_strings, flattened_translations):
|
||||
"""Recursively processes all translation strings for the integration."""
|
||||
result = {}
|
||||
|
@ -7,10 +7,11 @@ import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
from typing import Any
|
||||
|
||||
from .const import CLI_2_DOCKER_IMAGE, CORE_PROJECT_ID, INTEGRATIONS_DIR
|
||||
from .error import ExitApp
|
||||
from .util import get_lokalise_token, load_json_from_path
|
||||
from .util import flatten_translations, get_lokalise_token, load_json_from_path
|
||||
|
||||
FILENAME_FORMAT = re.compile(r"strings\.(?P<suffix>\w+)\.json")
|
||||
DOWNLOAD_DIR = Path("build/translations-download").absolute()
|
||||
@ -103,7 +104,15 @@ def save_language_translations(lang, translations):
|
||||
f"Skipping {lang} for {component}, as the integration doesn't seem to exist."
|
||||
)
|
||||
continue
|
||||
if not (
|
||||
Path("homeassistant") / "components" / component / "strings.json"
|
||||
).exists():
|
||||
print(
|
||||
f"Skipping {lang} for {component}, as the integration doesn't have a strings.json file."
|
||||
)
|
||||
continue
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
base_translations = pick_keys(component, base_translations)
|
||||
save_json(path, base_translations)
|
||||
|
||||
if "platform" not in component_translations:
|
||||
@ -131,6 +140,32 @@ def delete_old_translations():
|
||||
fil.unlink()
|
||||
|
||||
|
||||
def get_current_keys(component: str) -> dict[str, Any]:
|
||||
"""Get the current keys for a component."""
|
||||
strings_path = Path("homeassistant") / "components" / component / "strings.json"
|
||||
return load_json_from_path(strings_path)
|
||||
|
||||
|
||||
def pick_keys(component: str, translations: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Pick the keys that are in the current strings."""
|
||||
flat_translations = flatten_translations(translations)
|
||||
flat_current_keys = flatten_translations(get_current_keys(component))
|
||||
flatten_result = {}
|
||||
for key in flat_current_keys:
|
||||
if key in flat_translations:
|
||||
flatten_result[key] = flat_translations[key]
|
||||
result = {}
|
||||
for key, value in flatten_result.items():
|
||||
parts = key.split("::")
|
||||
d = result
|
||||
for part in parts[:-1]:
|
||||
if part not in d:
|
||||
d[part] = {}
|
||||
d = d[part]
|
||||
d[parts[-1]] = value
|
||||
return result
|
||||
|
||||
|
||||
def run():
|
||||
"""Run the script."""
|
||||
DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
@ -66,3 +66,26 @@ def load_json_from_path(path: pathlib.Path) -> Any:
|
||||
return json.loads(path.read_text())
|
||||
except json.JSONDecodeError as err:
|
||||
raise JSONDecodeErrorWithPath(err.msg, err.doc, err.pos, path) from err
|
||||
|
||||
|
||||
def flatten_translations(translations):
|
||||
"""Flatten all translations."""
|
||||
stack = [iter(translations.items())]
|
||||
key_stack = []
|
||||
flattened_translations = {}
|
||||
while stack:
|
||||
for k, v in stack[-1]:
|
||||
key_stack.append(k)
|
||||
if isinstance(v, dict):
|
||||
stack.append(iter(v.items()))
|
||||
break
|
||||
if isinstance(v, str):
|
||||
common_key = "::".join(key_stack)
|
||||
flattened_translations[common_key] = v
|
||||
key_stack.pop()
|
||||
else:
|
||||
stack.pop()
|
||||
if key_stack:
|
||||
key_stack.pop()
|
||||
|
||||
return flattened_translations
|
||||
|
Loading…
x
Reference in New Issue
Block a user