refactor(website): share SVG translation label selection (#17369)

This commit is contained in:
Vincent Koc
2026-12-06 11:07:02 +08:00
committed by GitHub
parent ee102492d1
commit e50f098e04
+17 -12
View File
@@ -335,6 +335,16 @@ def read_runtime(root, page=None):
return list(dict.fromkeys(text for script, text, _, _ in units if script in scripts))
def figure_units(source, name):
"""Yield gettext keys and source nodes for translatable SVG labels."""
context = "figure:" + name
for node in Document(source).nodes:
if node.tag in {"text", "title", "desc"} and not any(isinstance(child, Node) for child in node.children):
text = unescape(source[node.open_end:node.end_start]).strip()
if translatable(text):
yield (context, text), node
def source_catalog(root):
"""Build a deterministic POT with source references and protected-placeholder notes."""
site = root / "website/public"
@@ -366,11 +376,8 @@ def source_catalog(root):
entries[("runtime", text)].comment = note
for path in sorted((site / "assets/figures").glob("*.svg")):
source = path.read_text(encoding="utf-8")
for node in Document(source).nodes:
if node.tag in {"text", "title", "desc"} and not any(isinstance(child, Node) for child in node.children):
text = unescape(source[node.open_end:node.end_start]).strip()
if translatable(text):
add("figure:" + path.name, text, {}, "website/public/assets/figures/" + path.name, node.line)
for (context, text), node in figure_units(source, path.name):
add(context, text, {}, "website/public/assets/figures/" + path.name, node.line)
return catalog
@@ -665,13 +672,11 @@ def build_translations(root, destination, finalize=None):
figures = set()
for path in sorted((site / "assets/figures").glob("*.svg")):
source = path.read_text(encoding="utf-8")
edits = []
for node in Document(source).nodes:
if node.tag in {"text", "title", "desc"} and not any(isinstance(child, Node) for child in node.children):
text = unescape(source[node.open_end:node.end_start]).strip()
if translatable(text) and ("figure:" + path.name, text) in messages:
translated = messages[("figure:" + path.name, text)]
edits.append((node.open_end, node.end_start, escape(translated, quote=False)))
edits = [
(node.open_end, node.end_start, escape(messages[key], quote=False))
for key, node in figure_units(source, path.name)
if key in messages
]
translated = apply_edits(source, edits)
output = staging / "assets/localized" / language["code"] / path.relative_to(site / "assets")
output.parent.mkdir(parents=True, exist_ok=True)