From 32c99f06e19bf2d62358d5d617ec8509ce14c53e Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 15 Jan 2025 20:03:39 +0200 Subject: [PATCH] feat(core): allow merging translations from per-layout JSONs --- core/translations/crowdin.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/core/translations/crowdin.py b/core/translations/crowdin.py index 64fac3bcbd5..f296cec670b 100644 --- a/core/translations/crowdin.py +++ b/core/translations/crowdin.py @@ -1,4 +1,7 @@ +from __future__ import annotations + from pathlib import Path +import collections import json import click @@ -46,5 +49,35 @@ def prepare() -> None: click.echo(f"Successfully generated layout-specific translation files in '{CROWDIN_DIR}'") +@cli.command() +def merge() -> None: + """Merge back translation files downloaded from Crowdin.""" + tdir = TranslationsDir() + + for lang in sorted(tdir.all_languages()): + merged_translations: dict[str, str | dict[str, str]] = collections.defaultdict(dict) + for layout_type in ALL_LAYOUTS: + with open(CROWDIN_DIR / f"{lang}_{layout_type.name}.json", "r") as f: + blob_json = json.load(f) + + # mapping string name to its translation (for the current layout) + layout_specific_translations: dict[str, str] = blob_json["translations"] + for key, value in layout_specific_translations.items(): + merged_translations[key][layout_type.name] = value + + for key in merged_translations.keys(): + # deduplicate entries if all translations are the same + unique_translations = set(merged_translations[key].values()) + if len(unique_translations) == 1: + merged_translations[key] = unique_translations.pop() + + blob_json = tdir.load_lang(lang) + blob_json["translations"] = merged_translations + tdir.save_lang(lang, blob_json) + click.echo(f"Updated {lang}") + + click.echo(f"Successfully merged back layout-specific translation files from '{CROWDIN_DIR}'") + + if __name__ == "__main__": cli()