From a616a10faf59be88224e0ece96c4b8e380a9b84d Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 14 Jan 2025 18:29:09 +0200 Subject: [PATCH] feat(python): support per-layout translated text strings [no changelog] --- python/src/trezorlib/_internal/translations.py | 12 +++++++++++- python/src/trezorlib/debuglink.py | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/python/src/trezorlib/_internal/translations.py b/python/src/trezorlib/_internal/translations.py index 809725119d..42fcfc3f75 100644 --- a/python/src/trezorlib/_internal/translations.py +++ b/python/src/trezorlib/_internal/translations.py @@ -10,6 +10,7 @@ import construct as c from construct_classes import Struct, subcon from typing_extensions import Self, TypedDict +from ..debuglink import LayoutType from ..firmware.models import Model from ..models import TrezorModel from ..tools import EnumAdapter, TupleAdapter @@ -297,6 +298,14 @@ def order_from_json(json_order: dict[str, str]) -> Order: return {int(k): v for k, v in json_order.items()} +def get_translation(lang_data: JsonDef, key: str, layout_type: LayoutType) -> str: + item = lang_data["translations"].get(key, "") + if isinstance(item, dict): + return item.get(layout_type.name, "") + + return item # Same translation for all layouts + + def blob_from_defs( lang_data: JsonDef, order: Order, @@ -305,10 +314,11 @@ def blob_from_defs( fonts_dir: Path, ) -> TranslationsBlob: json_header: JsonHeader = lang_data["header"] + layout_type = LayoutType.from_model(model) # order translations -- python dicts keep insertion order translations_ordered: list[str] = [ - lang_data["translations"].get(key, "") for _, key in sorted(order.items()) + get_translation(lang_data, key, layout_type) for _, key in sorted(order.items()) ] translations = TranslatedStrings.from_items(translations_ordered) diff --git a/python/src/trezorlib/debuglink.py b/python/src/trezorlib/debuglink.py index d1f89db35f..a7434cb106 100644 --- a/python/src/trezorlib/debuglink.py +++ b/python/src/trezorlib/debuglink.py @@ -79,6 +79,7 @@ class LayoutType(Enum): Bolt = auto() Samson = auto() Quicksilver = auto() + LayoutTBD = auto() @classmethod def from_model(cls, model: models.TrezorModel) -> "LayoutType": @@ -88,6 +89,8 @@ class LayoutType(Enum): return cls.Samson if model in (models.T3T1,): return cls.Quicksilver + if model in (models.T3W1,): + return cls.LayoutTBD # TODO: use correct layout for T3W1 if model in (models.T1B1,): return cls.T1 raise ValueError(f"Unknown model: {model}")