Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): allow splitting translations into per-layout JSONs #4503

Open
wants to merge 4 commits into
base: romanz/translations
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 69 additions & 41 deletions core/translations/crowdin.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,83 @@
import subprocess
import tempfile
from __future__ import annotations

from pathlib import Path
import collections
import json
import sys
import os

import click

from cli import TranslationsDir
from trezorlib._internal import translations
from trezorlib.debuglink import LayoutType


HERE = Path(__file__).parent

# staging directory for layout-specific translation JSON files
CROWDIN_DIR = HERE / "crowdin"

# layouts with translation support
ALL_LAYOUTS = frozenset(LayoutType) - {LayoutType.T1}


@click.group()
def cli() -> None:
pass


@cli.command()
def prepare() -> None:
"""Prepare translation files for Crowdin upload.

Create a separate JSON file for each language and layout.
"""
tdir = TranslationsDir()

for lang in tdir.all_languages():
blob_json = tdir.load_lang(lang)
for layout_type in ALL_LAYOUTS:
# extract translations specific to this layout
layout_specific_translations = {
key: translations.get_translation(blob_json, key, layout_type)
for key in blob_json["translations"].keys()
}
# create a JSON file with only the "translations" item
result = {"translations": layout_specific_translations}
with open(CROWDIN_DIR / f"{lang}_{layout_type.name}.json", "w") as f:
json.dump(result, f, indent=2, ensure_ascii=False)

click.echo(f"Successfully generated layout-specific translation files in '{CROWDIN_DIR}'")


def download() -> None:
with tempfile.TemporaryDirectory() as temp_dir:
command = f"crowdin download --all --verbose --token $CROWDIN_TOKEN --base-path={temp_dir}"
print("command", command)
@cli.command()
def merge() -> None:
"""Merge back translation files downloaded from Crowdin."""
tdir = TranslationsDir()

subprocess.run(command, shell=True, check=True)
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)

for directory in Path(temp_dir).iterdir():
print("directory", directory)
lang_name = directory.name
en_file = directory / "en.json"
if not en_file.exists():
print("Skipping - no en.json inside", lang_name)
continue
print("Processing", lang_name)
data = json.loads(en_file.read_text())
lang_file = HERE / f"{lang_name}.json"
if not lang_file.exists():
print("Skipping - no lang_file on our side", lang_name)
continue
lang_file_data = json.loads(lang_file.read_text())
lang_file_data["translations"] = data["translations"]
lang_file.write_text(json.dumps(lang_file_data, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
print("Translations updated", lang_name)
# 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()

def upload() -> None:
command = "crowdin upload sources --token $CROWDIN_TOKEN"
print("command", command)
blob_json = tdir.load_lang(lang)
blob_json["translations"] = merged_translations
tdir.save_lang(lang, blob_json)
click.echo(f"Updated {lang}")

subprocess.run(command, shell=True, check=True)
click.echo(f"Successfully merged back layout-specific translation files from '{CROWDIN_DIR}'")


if __name__ == "__main__":
if not os.environ.get("CROWDIN_TOKEN"):
print("CROWDIN_TOKEN env variable not set")
sys.exit(1)

if "download" in sys.argv:
download()
elif "upload" in sys.argv:
upload()
else:
print("Usage: python crowdin.py [download|upload]")
sys.exit(1)
cli()
1 change: 1 addition & 0 deletions core/translations/crowdin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
31 changes: 18 additions & 13 deletions core/translations/cs.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"fonts": {
"Samson": {
"1_FONT_NORMAL": "font_pixeloperator_regular_8_cs.json",
"2_FONT_BOLD": "font_pixeloperator_bold_8_cs.json",
"3_FONT_MONO": "font_pixeloperatormono_regular_8_cs.json",
"4_FONT_BIG": "font_unifont_regular_16_cs.json",
"5_FONT_DEMIBOLD": "font_unifont_bold_16_cs.json",
"6_FONT_NORMAL_UPPER": "font_pixeloperator_regular_8_upper_cs.json",
"7_FONT_BOLD_UPPER": "font_pixeloperator_bold_8_upper_cs.json",
"8_FONT_SUB": null
},
"Bolt": {
"1_FONT_NORMAL": "font_tthoves_regular_21_cs.json",
"2_FONT_BOLD": null,
Expand All @@ -20,7 +10,7 @@
"7_FONT_BOLD_UPPER": "font_tthoves_bold_17_upper_cs.json",
"8_FONT_SUB": null
},
"Quicksilver": {
"LayoutTBD": {
"1_FONT_NORMAL": "font_ttsatoshi_demibold_21_cs.json",
"2_FONT_BOLD": "font_ttsatoshi_demibold_21_cs.json",
"3_FONT_MONO": "font_robotomono_medium_21_cs.json",
Expand All @@ -30,7 +20,7 @@
"7_FONT_BOLD_UPPER": null,
"8_FONT_SUB": "font_ttsatoshi_demibold_18_cs.json"
},
"LayoutTBD": {
"Quicksilver": {
"1_FONT_NORMAL": "font_ttsatoshi_demibold_21_cs.json",
"2_FONT_BOLD": "font_ttsatoshi_demibold_21_cs.json",
"3_FONT_MONO": "font_robotomono_medium_21_cs.json",
Expand All @@ -39,6 +29,16 @@
"6_FONT_NORMAL_UPPER": null,
"7_FONT_BOLD_UPPER": null,
"8_FONT_SUB": "font_ttsatoshi_demibold_18_cs.json"
},
"Samson": {
"1_FONT_NORMAL": "font_pixeloperator_regular_8_cs.json",
"2_FONT_BOLD": "font_pixeloperator_bold_8_cs.json",
"3_FONT_MONO": "font_pixeloperatormono_regular_8_cs.json",
"4_FONT_BIG": "font_unifont_regular_16_cs.json",
"5_FONT_DEMIBOLD": "font_unifont_bold_16_cs.json",
"6_FONT_NORMAL_UPPER": "font_pixeloperator_regular_8_upper_cs.json",
"7_FONT_BOLD_UPPER": "font_pixeloperator_bold_8_upper_cs.json",
"8_FONT_SUB": null
}
},
"header": {
Expand Down Expand Up @@ -421,7 +421,12 @@
"homescreen__title_no_usb_connection": "Žádné připojení usb",
"homescreen__title_pin_not_set": "Není nastaven PIN",
"homescreen__title_seedless": "Bez seedu",
"homescreen__title_set": {"Bolt": "Změnit domov. obr.?", "Samson": "Změnit domov. obr.?", "Quicksilver": "Změnit domovskou obrazovku?", "LayoutTBD": "Změnit domovskou obrazovku?"},
"homescreen__title_set": {
"Bolt": "Změnit domov. obr.?",
"LayoutTBD": "Změnit domovskou obrazovku?",
"Quicksilver": "Změnit domovskou obrazovku?",
"Samson": "Změnit domov. obr.?"
},
"inputs__back": "ZPĚT",
"inputs__cancel": "ZRUŠIT",
"inputs__delete": "ODSTRANIT",
Expand Down
24 changes: 12 additions & 12 deletions core/translations/de.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"fonts": {
"Samson": {
"1_FONT_NORMAL": "font_pixeloperator_regular_8_de.json",
"2_FONT_BOLD": "font_pixeloperator_bold_8_de.json",
"3_FONT_MONO": "font_pixeloperatormono_regular_8_de.json",
"4_FONT_BIG": "font_unifont_regular_16_de.json",
"5_FONT_DEMIBOLD": "font_unifont_bold_16_de.json",
"6_FONT_NORMAL_UPPER": "font_pixeloperator_regular_8_upper_de.json",
"7_FONT_BOLD_UPPER": "font_pixeloperator_bold_8_upper_de.json",
"8_FONT_SUB": null
},
"Bolt": {
"1_FONT_NORMAL": "font_tthoves_regular_21_de.json",
"2_FONT_BOLD": null,
Expand All @@ -20,7 +10,7 @@
"7_FONT_BOLD_UPPER": "font_tthoves_bold_17_upper_de.json",
"8_FONT_SUB": null
},
"Quicksilver": {
"LayoutTBD": {
"1_FONT_NORMAL": "font_ttsatoshi_demibold_21_de.json",
"2_FONT_BOLD": "font_ttsatoshi_demibold_21_de.json",
"3_FONT_MONO": "font_robotomono_medium_21_de.json",
Expand All @@ -30,7 +20,7 @@
"7_FONT_BOLD_UPPER": null,
"8_FONT_SUB": "font_ttsatoshi_demibold_18_de.json"
},
"LayoutTBD": {
"Quicksilver": {
"1_FONT_NORMAL": "font_ttsatoshi_demibold_21_de.json",
"2_FONT_BOLD": "font_ttsatoshi_demibold_21_de.json",
"3_FONT_MONO": "font_robotomono_medium_21_de.json",
Expand All @@ -39,6 +29,16 @@
"6_FONT_NORMAL_UPPER": null,
"7_FONT_BOLD_UPPER": null,
"8_FONT_SUB": "font_ttsatoshi_demibold_18_de.json"
},
"Samson": {
"1_FONT_NORMAL": "font_pixeloperator_regular_8_de.json",
"2_FONT_BOLD": "font_pixeloperator_bold_8_de.json",
"3_FONT_MONO": "font_pixeloperatormono_regular_8_de.json",
"4_FONT_BIG": "font_unifont_regular_16_de.json",
"5_FONT_DEMIBOLD": "font_unifont_bold_16_de.json",
"6_FONT_NORMAL_UPPER": "font_pixeloperator_regular_8_upper_de.json",
"7_FONT_BOLD_UPPER": "font_pixeloperator_bold_8_upper_de.json",
"8_FONT_SUB": null
}
},
"header": {
Expand Down
12 changes: 10 additions & 2 deletions core/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,11 @@
"fido__title_reset": "FIDO2 reset",
"fido__title_select_credential": "Select credential",
"fido__title_u2f_auth": "U2F authenticate",
"fido__title_u2f_register": {"Bolt": "U2F register", "Samson": "U2F register", "Quicksilver": "U2F register"},
"fido__title_u2f_register": {
"Bolt": "U2F register",
"Quicksilver": "U2F register",
"Samson": "U2F register"
},
"fido__title_verify_user": "FIDO2 verify user",
"fido__unable_to_verify_user": "Unable to verify user.",
"fido__wanna_erase_credentials": "Do you really want to erase all credentials?",
Expand All @@ -379,7 +383,11 @@
"homescreen__title_no_usb_connection": "No USB connection",
"homescreen__title_pin_not_set": "PIN not set",
"homescreen__title_seedless": "Seedless",
"homescreen__title_set": {"Bolt": "Change wallpaper?", "Samson": "Change wallpaper?", "Quicksilver": "Change wallpaper?"},
"homescreen__title_set": {
"Bolt": "Change wallpaper?",
"Quicksilver": "Change wallpaper?",
"Samson": "Change wallpaper?"
},
"inputs__back": "BACK",
"inputs__cancel": "CANCEL",
"inputs__delete": "DELETE",
Expand Down
24 changes: 12 additions & 12 deletions core/translations/es.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"fonts": {
"Samson": {
"1_FONT_NORMAL": "font_pixeloperator_regular_8_es.json",
"2_FONT_BOLD": "font_pixeloperator_bold_8_es.json",
"3_FONT_MONO": "font_pixeloperatormono_regular_8_es.json",
"4_FONT_BIG": "font_unifont_regular_16_es.json",
"5_FONT_DEMIBOLD": "font_unifont_bold_16_es.json",
"6_FONT_NORMAL_UPPER": "font_pixeloperator_regular_8_upper_es.json",
"7_FONT_BOLD_UPPER": "font_pixeloperator_bold_8_upper_es.json",
"8_FONT_SUB": null
},
"Bolt": {
"1_FONT_NORMAL": "font_tthoves_regular_21_es.json",
"2_FONT_BOLD": null,
Expand All @@ -20,7 +10,7 @@
"7_FONT_BOLD_UPPER": "font_tthoves_bold_17_upper_es.json",
"8_FONT_SUB": null
},
"Quicksilver": {
"LayoutTBD": {
"1_FONT_NORMAL": "font_ttsatoshi_demibold_21_es.json",
"2_FONT_BOLD": "font_ttsatoshi_demibold_21_es.json",
"3_FONT_MONO": "font_robotomono_medium_21_es.json",
Expand All @@ -30,7 +20,7 @@
"7_FONT_BOLD_UPPER": null,
"8_FONT_SUB": "font_ttsatoshi_demibold_18_es.json"
},
"LayoutTBD": {
"Quicksilver": {
"1_FONT_NORMAL": "font_ttsatoshi_demibold_21_es.json",
"2_FONT_BOLD": "font_ttsatoshi_demibold_21_es.json",
"3_FONT_MONO": "font_robotomono_medium_21_es.json",
Expand All @@ -39,6 +29,16 @@
"6_FONT_NORMAL_UPPER": null,
"7_FONT_BOLD_UPPER": null,
"8_FONT_SUB": "font_ttsatoshi_demibold_18_es.json"
},
"Samson": {
"1_FONT_NORMAL": "font_pixeloperator_regular_8_es.json",
"2_FONT_BOLD": "font_pixeloperator_bold_8_es.json",
"3_FONT_MONO": "font_pixeloperatormono_regular_8_es.json",
"4_FONT_BIG": "font_unifont_regular_16_es.json",
"5_FONT_DEMIBOLD": "font_unifont_bold_16_es.json",
"6_FONT_NORMAL_UPPER": "font_pixeloperator_regular_8_upper_es.json",
"7_FONT_BOLD_UPPER": "font_pixeloperator_bold_8_upper_es.json",
"8_FONT_SUB": null
}
},
"header": {
Expand Down
Loading
Loading