Skip to content

Commit

Permalink
feat(core): allow merging translations from per-layout JSONs
Browse files Browse the repository at this point in the history
  • Loading branch information
romanz committed Jan 15, 2025
1 parent 3c44637 commit 32c99f0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions core/translations/crowdin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

from pathlib import Path
import collections
import json

import click
Expand Down Expand Up @@ -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()

0 comments on commit 32c99f0

Please sign in to comment.