1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-07 05:02:38 +00:00

feat(python): check that translations are using correct layout names

[no changelog]
This commit is contained in:
Roman Zeyde 2025-01-28 11:01:14 +02:00 committed by Roman Zeyde
parent 78768950fd
commit 5cdd0e7e74
3 changed files with 35 additions and 10 deletions

View File

@ -159,11 +159,10 @@ class TranslationsDir:
def generate_single_blob(
self,
lang: str,
blob_json: translations.JsonDef,
model: models.TrezorModel,
version: VersionTuple | None,
) -> translations.TranslationsBlob:
blob_json = self.load_lang(lang)
blob_version = translations.version_from_json(blob_json["header"]["version"])
return translations.blob_from_defs(
blob_json, self.order, model, version or blob_version, self.fonts_dir
@ -179,9 +178,12 @@ class TranslationsDir:
if lang == "en":
continue
blob_json = self.load_lang(lang)
translations.check_blob(blob_json)
for model in ALL_MODELS:
try:
blob = self.generate_single_blob(lang, model, version)
blob = self.generate_single_blob(blob_json, model, version)
blob_version = blob.header.firmware_version
if common_version is None:
common_version = blob_version

View File

@ -8,7 +8,6 @@ import click
from cli import TranslationsDir
from trezorlib._internal import translations
from trezorlib.debuglink import LayoutType
HERE = Path(__file__).parent
@ -16,10 +15,6 @@ 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
@ -35,7 +30,7 @@ def split() -> None:
for lang in tdir.all_languages():
blob_json = tdir.load_lang(lang)
for layout_type in ALL_LAYOUTS:
for layout_type in translations.ALL_LAYOUTS:
# extract translations specific to this layout
layout_specific_translations = {
key: translations.get_translation(blob_json, key, layout_type)
@ -56,7 +51,7 @@ def merge() -> None:
for lang in sorted(tdir.all_languages()):
merged_translations: dict[str, str | dict[str, str]] = collections.defaultdict(dict)
for layout_type in ALL_LAYOUTS:
for layout_type in translations.ALL_LAYOUTS:
with open(CROWDIN_DIR / f"{lang}_{layout_type.name}.json", "r") as f:
blob_json = json.load(f)

View File

@ -293,6 +293,34 @@ class TranslationsBlob(Struct):
# ====================
# layouts with translation support
ALL_LAYOUTS = frozenset(LayoutType) - {LayoutType.T1}
ALL_LAYOUT_NAMES = frozenset(layout.name for layout in ALL_LAYOUTS)
def check_blob(lang_data: JsonDef):
json_header: JsonHeader = lang_data["header"]
lang_version = f"{json_header['language']} v{json_header['version']}"
font_layout_names = set(lang_data["fonts"].keys())
if font_layout_names != ALL_LAYOUT_NAMES:
raise ValueError(
f"Invalid font layout names for {lang_version}: {font_layout_names}"
)
for key, item in lang_data["translations"].items():
if isinstance(item, dict):
item_layouts = set(item.keys())
unknown_layouts = item_layouts - ALL_LAYOUT_NAMES
missing_layouts = ALL_LAYOUT_NAMES - item_layouts
if unknown_layouts or missing_layouts:
raise ValueError(
f"Invalid translation layouts for {lang_version}: {key}"
f"\nUnknown layouts: {list(unknown_layouts)}"
f"\nMissing layouts: {list(missing_layouts)}"
)
def order_from_json(json_order: dict[str, str]) -> Order:
return {int(k): v for k, v in json_order.items()}