Skip to content

Commit

Permalink
feat(python): support per-layout translated text strings
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
romanz committed Jan 14, 2025
1 parent 56f0806 commit a616a10
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 11 additions & 1 deletion python/src/trezorlib/_internal/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions python/src/trezorlib/debuglink.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class LayoutType(Enum):
Bolt = auto()
Samson = auto()
Quicksilver = auto()
LayoutTBD = auto()

@classmethod
def from_model(cls, model: models.TrezorModel) -> "LayoutType":
Expand All @@ -88,6 +89,8 @@ def from_model(cls, model: models.TrezorModel) -> "LayoutType":
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}")
Expand Down

0 comments on commit a616a10

Please sign in to comment.