From ca1c5c4e3334d6f72426b4c7b29dfc2565775e90 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 9 Jul 2024 10:17:36 +0200 Subject: [PATCH] feat(core/translations): cli.py merge command --- core/translations/cli.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/translations/cli.py b/core/translations/cli.py index 5a10441e8..067aabb07 100644 --- a/core/translations/cli.py +++ b/core/translations/cli.py @@ -133,6 +133,7 @@ class TranslationsDir: data, indent=2, ensure_ascii=False, + sort_keys=True, ) + "\n" ) @@ -365,5 +366,27 @@ def sign(signature_hex: str, force: bool | None, version_str: str | None) -> Non build_all_blobs(all_blobs, tree, sigmask, signature, production=True) +def _dict_merge(a: dict, b: dict) -> None: + for k, v in b.items(): + if k in a and isinstance(a[k], dict) and isinstance(v, dict): + _dict_merge(a[k], v) + else: + a[k] = v + + +@cli.command() +@click.argument("update_json", type=click.File("r"), nargs=-1) +def merge(update_json: t.Tuple[t.TextIO, ...]) -> None: + """Update translations from JSON files.""" + tdir = TranslationsDir() + for f in update_json: + new_data = json.load(f) + lang = new_data["header"]["language"][:2] + orig_data = tdir.load_lang(lang) + _dict_merge(orig_data, new_data) + tdir.save_lang(lang, orig_data) + click.echo(f"Updated {lang}") + + if __name__ == "__main__": cli()