|
| 1 | +import csv |
| 2 | +from pathlib import Path |
| 3 | +from ruamel.yaml import YAML # pip install ruamel.yaml |
| 4 | + |
| 5 | +# -------- CONFIG -------- |
| 6 | +TRANSLATION_CSV = r"C:\temp\translations.csv" |
| 7 | +LANGUAGE_KEY = "chinese_s" |
| 8 | +SOURCE_LANG = "en_us" |
| 9 | +# ------------------------ |
| 10 | + |
| 11 | +yaml = YAML() |
| 12 | + |
| 13 | + |
| 14 | +def build_lookup(csv_path): |
| 15 | + """Build dictionary: {yaml_file: {key_type: {"en_us":..., "translation":...}}}""" |
| 16 | + lookup = {} |
| 17 | + |
| 18 | + with open(csv_path, encoding="utf-8") as f: |
| 19 | + reader = csv.DictReader(f) |
| 20 | + |
| 21 | + for row in reader: |
| 22 | + file = row["yaml_file"] |
| 23 | + key = row["key_type"] |
| 24 | + |
| 25 | + en = row[SOURCE_LANG] |
| 26 | + tr = row[LANGUAGE_KEY] |
| 27 | + |
| 28 | + lookup.setdefault(file, {}).setdefault(key, {}) |
| 29 | + lookup[file][key] = {"en": en, "tr": tr} |
| 30 | + |
| 31 | + return lookup |
| 32 | + |
| 33 | + |
| 34 | +def insert_translation(data, key_type, trans, node_path=""): |
| 35 | + """Insert translation from CSV. |
| 36 | + Rule: write ONLY if CSV provides a non-empty translation; always overwrite.""" |
| 37 | + if not isinstance(data, dict): |
| 38 | + return |
| 39 | + |
| 40 | + translation = trans["tr"].strip() |
| 41 | + |
| 42 | + if key_type in data: |
| 43 | + value = data[key_type] |
| 44 | + |
| 45 | + # CASE A: scalar -> convert to dict |
| 46 | + if isinstance(value, str): |
| 47 | + print(f"[Scalar detected during import] {node_path}/{key_type} | Converting to multilingual") |
| 48 | + new_dict = {SOURCE_LANG: value} |
| 49 | + |
| 50 | + if translation: |
| 51 | + new_dict[LANGUAGE_KEY] = translation |
| 52 | + print(f"[Write] {node_path}/{key_type} | {LANGUAGE_KEY} = '{translation}'") |
| 53 | + else: |
| 54 | + print(f"[Skip] CSV translation empty for {node_path}/{key_type}") |
| 55 | + |
| 56 | + data[key_type] = new_dict |
| 57 | + return |
| 58 | + |
| 59 | + # CASE B: dict |
| 60 | + elif isinstance(value, dict): |
| 61 | + # ensure en_us exists |
| 62 | + if SOURCE_LANG not in value: |
| 63 | + if len(value): |
| 64 | + first_key = next(iter(value.keys())) |
| 65 | + value[SOURCE_LANG] = value[first_key] |
| 66 | + print(f"[Promotion] No {SOURCE_LANG} → using '{first_key}' value") |
| 67 | + |
| 68 | + # write only if CSV translation provided |
| 69 | + if translation: |
| 70 | + value[LANGUAGE_KEY] = translation |
| 71 | + print(f"[Write] {node_path}/{key_type} | Overwriting {LANGUAGE_KEY} = '{translation}'") |
| 72 | + else: |
| 73 | + print(f"[Skip] CSV translation empty for {node_path}/{key_type}") |
| 74 | + |
| 75 | + # recurse deeper |
| 76 | + for k, v in data.items(): |
| 77 | + child_path = f"{node_path}/{k}" if node_path else k |
| 78 | + insert_translation(v, key_type, trans, child_path) |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + lookup = build_lookup(TRANSLATION_CSV) |
| 83 | + |
| 84 | + for yaml_file, fields in lookup.items(): |
| 85 | + yaml_file = Path(yaml_file) |
| 86 | + |
| 87 | + if not yaml_file.exists(): |
| 88 | + print(f"[Missing] {yaml_file}") |
| 89 | + continue |
| 90 | + |
| 91 | + try: |
| 92 | + with open(yaml_file, "r", encoding="utf-8") as f: |
| 93 | + data = yaml.load(f) |
| 94 | + |
| 95 | + for key_type, trans in fields.items(): |
| 96 | + insert_translation(data, key_type, trans) |
| 97 | + |
| 98 | + with open(yaml_file, "w", encoding="utf-8") as f: |
| 99 | + yaml.dump(data, f) |
| 100 | + |
| 101 | + print(f"[Updating] {yaml_file}") |
| 102 | + |
| 103 | + except Exception as e: |
| 104 | + print(f"[ERROR] writing {yaml_file}: {e}") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments