Skip to content

Commit

Permalink
WIP - specific fonts for specific models
Browse files Browse the repository at this point in the history
  • Loading branch information
grdddj committed Nov 6, 2023
1 parent 22cb18c commit c9e8dad
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
7 changes: 6 additions & 1 deletion core/embed/rust/src/ui/translations/cs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"font": {
"file": "font_pixeloperator_regular_8_cs.json"
"T": {
"file": "font_roboto_regular_20_cs.json"
},
"Safe 3": {
"file": "font_pixeloperator_regular_8_cs.json"
}
},
"header": {
"language": "cs",
Expand Down
7 changes: 6 additions & 1 deletion core/embed/rust/src/ui/translations/fr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"font": {
"file": "font_pixeloperator_regular_8_fr.json"
"T": {
"file": "font_roboto_regular_20_fr.json"
},
"Safe 3": {
"file": "font_pixeloperator_regular_8_fr.json"
}
},
"header": {
"language": "fr",
Expand Down
4 changes: 3 additions & 1 deletion python/src/trezorlib/cli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ def language(client: "TrezorClient", file: TextIO, url: str, remove: bool) -> st
language_data = b""
else:
if file:
language_data = translations.blob_from_file(file)
model = client.features.model
assert model is not None
language_data = translations.blob_from_file(file, model)
elif url:
language_data = translations.blob_from_url(url)
else:
Expand Down
13 changes: 8 additions & 5 deletions python/src/trezorlib/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
FontData = Dict[str, str]


def blob_from_file(file: TextIO) -> bytes:
def blob_from_file(file: TextIO, model: str) -> bytes:
data = json.load(file)
file_dir = file.name.rsplit("/", 1)[0]
file_dir_absolute_path = Path(file_dir).absolute()
return blob_from_dict(data, file_dir_absolute_path)
return blob_from_dict(data, file_dir_absolute_path, model)


def blob_from_url(url: str) -> bytes:
Expand All @@ -27,11 +27,14 @@ def blob_from_url(url: str) -> bytes:
return r.content


def blob_from_dict(data: Dict[str, Any], file_dir: Path) -> bytes:
def blob_from_dict(data: Dict[str, Any], file_dir: Path, model: str) -> bytes:
header: HeaderData = data["header"]
translations: TranslationData = data["translations"]
font: FontData = data["font"]
return _blob_from_data(header, translations, font, file_dir)
font = data["font"]
if model not in font:
raise ValueError(f"Font for model {model} not found")
model_font: FontData = font[model]
return _blob_from_data(header, translations, model_font, file_dir)


def _blob_from_data(
Expand Down
31 changes: 25 additions & 6 deletions tests/device_tests/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ def _set_english_return_back(client: Client) -> Generator[Client, None, None]:

def _set_full_czech(client: Client):
with client, open(CS_JSON, "r") as f:
device.change_language(client, language_data=translations.blob_from_file(f))
device.change_language(
client,
language_data=translations.blob_from_file(f, client.features.model or ""),
)


def _set_full_french(client: Client):
with client, open(FR_JSON, "r") as f:
device.change_language(client, language_data=translations.blob_from_file(f))
device.change_language(
client,
language_data=translations.blob_from_file(f, client.features.model or ""),
)


def _set_default_english(client: Client):
Expand Down Expand Up @@ -96,15 +102,23 @@ def test_change_language_errors(client: Client):
), client:
with open(CS_JSON, "r") as f:
device.change_language(
client, language_data=translations.blob_from_file(f) + b"abc"
client,
language_data=translations.blob_from_file(
f, client.features.model or ""
)
+ b"abc",
)
assert client.features.language == "en-US"

# Invalid data hash
with pytest.raises(exceptions.TrezorFailure, match="Invalid data hash"), client:
with open(CS_JSON, "r") as f:
device.change_language(
client, language_data=translations.blob_from_file(f)[:-4] + b"abcd"
client,
language_data=translations.blob_from_file(
f, client.features.model or ""
)[:-4]
+ b"abcd",
)
assert client.features.language == "en-US"

Expand All @@ -117,7 +131,9 @@ def test_change_language_errors(client: Client):
data["header"]["version"] = "3.5.4"
device.change_language(
client,
language_data=translations.blob_from_dict(data, file_dir=TRANSLATIONS),
language_data=translations.blob_from_dict(
data, file_dir=TRANSLATIONS, model=client.features.model or ""
),
)
assert client.features.language == "en-US"

Expand All @@ -129,7 +145,10 @@ def test_change_language_errors(client: Client):
data = json.load(f)
data["header"]["version"] = "ABC.XYZ.DEF"
device.change_language(
client, language_data=translations.blob_from_dict(data, file_dir=TRANSLATIONS)
client,
language_data=translations.blob_from_dict(
data, file_dir=TRANSLATIONS, model=client.features.model or ""
),
)
assert client.features.language == "en-US"

Expand Down

0 comments on commit c9e8dad

Please sign in to comment.