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

39 lines
1.4 KiB
Python

"""
translation_sync_common.py - Shared merge/write logic for the *_translations sync
scripts (sync_dialogue_translations.py, sync_cutscene_translations.py).
Each sync script collects strings from its own JSON shape (dialogue nodes/choices,
cutscene lines, ...) and hands the ordered list of unique strings to
sync_translations(), which merges them into a translations JSON file shaped as
{"dialogue_translations": [{"key":..., "ru":..., "en":...}, ...]}. New entries are
always appended at the end so missing translations are easy to locate.
"""
import json
import os
def sync_translations(strings: list[str], translations_path: str) -> None:
if os.path.isfile(translations_path):
with open(translations_path, 'r', encoding='utf-8') as f:
translations = json.load(f)
else:
translations = {}
entries = translations.setdefault("dialogue_translations", [])
existing_keys = {entry.get("key") for entry in entries}
added = 0
for text in strings:
if text in existing_keys:
continue
entries.append({"key": text, "ru": text, "en": ""})
existing_keys.add(text)
added += 1
os.makedirs(os.path.dirname(os.path.abspath(translations_path)), exist_ok=True)
with open(translations_path, 'w', encoding='utf-8') as f:
json.dump(translations, f, indent=4, ensure_ascii=False)
print(f"Saved: {translations_path} ({added} new key(s) added, {len(entries)} total)")