mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Fix translation references to unverified translations (#155314)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
@@ -3,12 +3,11 @@
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
from . import download, upload
|
||||
from .const import INTEGRATIONS_DIR
|
||||
from .util import flatten_translations, get_base_arg_parser
|
||||
from .util import flatten_translations, get_base_arg_parser, substitute_references
|
||||
|
||||
|
||||
def valid_integration(integration):
|
||||
@@ -31,42 +30,6 @@ def get_arguments() -> argparse.Namespace:
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def substitute_translation_references(integration_strings, flattened_translations):
|
||||
"""Recursively processes all translation strings for the integration."""
|
||||
result = {}
|
||||
for key, value in integration_strings.items():
|
||||
if isinstance(value, dict):
|
||||
sub_dict = substitute_translation_references(value, flattened_translations)
|
||||
result[key] = sub_dict
|
||||
elif isinstance(value, str):
|
||||
result[key] = substitute_reference(value, flattened_translations)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def substitute_reference(value, flattened_translations):
|
||||
"""Substitute localization key references in a translation string."""
|
||||
matches = re.findall(r"\[\%key:([a-z0-9_]+(?:::(?:[a-z0-9-_])+)+)\%\]", value)
|
||||
if not matches:
|
||||
return value
|
||||
|
||||
new = value
|
||||
for key in matches:
|
||||
if key in flattened_translations:
|
||||
new = new.replace(
|
||||
f"[%key:{key}%]",
|
||||
# New value can also be a substitution reference
|
||||
substitute_reference(
|
||||
flattened_translations[key], flattened_translations
|
||||
),
|
||||
)
|
||||
else:
|
||||
print(f"Invalid substitution key '{key}' found in string '{value}'")
|
||||
sys.exit(1)
|
||||
|
||||
return new
|
||||
|
||||
|
||||
def run_single(translations, flattened_translations, integration):
|
||||
"""Run the script for a single integration."""
|
||||
print(f"Generating translations for {integration}")
|
||||
@@ -77,8 +40,8 @@ def run_single(translations, flattened_translations, integration):
|
||||
|
||||
integration_strings = translations["component"][integration]
|
||||
|
||||
translations["component"][integration] = substitute_translation_references(
|
||||
integration_strings, flattened_translations
|
||||
translations["component"][integration] = substitute_references(
|
||||
integration_strings, flattened_translations, fail_on_missing=True
|
||||
)
|
||||
|
||||
if download.DOWNLOAD_DIR.is_dir():
|
||||
|
||||
Reference in New Issue
Block a user