shadow-over-bishkek001/sync_dialogue_translations.py
2026-07-05 21:52:37 +03:00

53 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""
sync_dialogue_translations.py - Sync a dialogue config's strings into a translation file.
Reads <dialogue_config.json>, collects every non-empty "speaker" and "text" string
from its nodes (and "text" from any node's "choices"), and appends any string not
already present as a "key" in <translations.json> as a new entry with an empty
"en" slot ready for a translator to fill in. New entries are always appended at
the end of the list so missing translations are easy to locate.
Usage:
python sync_dialogue_translations.py <dialogue_config.json> <translations.json>
"""
import json
import sys
from translation_sync_common import sync_translations
def collect_strings(dialogue_config: dict) -> list[str]:
seen = set()
ordered = []
def add(value):
if value and value not in seen:
seen.add(value)
ordered.append(value)
for dialogue in dialogue_config.get("dialogues", []):
for node in dialogue.get("nodes", []):
add(node.get("speaker", ""))
add(node.get("text", ""))
for choice in node.get("choices", []):
add(choice.get("text", ""))
return ordered
def sync(dialogue_path: str, translations_path: str) -> None:
with open(dialogue_path, 'r', encoding='utf-8') as f:
dialogue_config = json.load(f)
sync_translations(collect_strings(dialogue_config), translations_path)
if __name__ == '__main__':
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} <dialogue_config.json> <translations.json>")
sys.exit(1)
sync(sys.argv[1], sys.argv[2])