Skip to content

Commit a16adfb

Browse files
committed
add updater
1 parent eb0cd5c commit a16adfb

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

selfdrive/ui/layouts/home.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from openpilot.selfdrive.ui.widgets.setup import SetupWidget
1010
from openpilot.system.ui.lib.text_measure import measure_text_cached
1111
from openpilot.system.ui.lib.application import gui_app, FontWeight, MousePos
12+
from openpilot.system.ui.lib.multilang import tr
1213
from openpilot.system.ui.widgets.label import gui_label
1314
from openpilot.system.ui.widgets import Widget
1415

@@ -151,7 +152,7 @@ def _render_header(self):
151152
highlight_color = rl.Color(75, 95, 255, 255) if self.current_state == HomeLayoutState.UPDATE else rl.Color(54, 77, 239, 255)
152153
rl.draw_rectangle_rounded(self.update_notif_rect, 0.3, 10, highlight_color)
153154

154-
text = "UPDATE"
155+
text = tr("UPDATE")
155156
text_size = measure_text_cached(font, text, HEAD_BUTTON_FONT_SIZE)
156157
text_x = self.update_notif_rect.x + (self.update_notif_rect.width - text_size.x) // 2
157158
text_y = self.update_notif_rect.y + (self.update_notif_rect.height - text_size.y) // 2
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import json
4+
import os
5+
6+
from openpilot.common.basedir import BASEDIR
7+
8+
UI_DIR = os.path.join(BASEDIR, "selfdrive", "ui")
9+
TRANSLATIONS_DIR = os.path.join(UI_DIR, "translations")
10+
LANGUAGES_FILE = os.path.join(TRANSLATIONS_DIR, "languages.json")
11+
12+
13+
def update_translations():
14+
files = []
15+
for root, _, filenames in os.walk(os.path.join(UI_DIR, "widgets")):
16+
for filename in filenames:
17+
if filename.endswith(".py"):
18+
files.append(os.path.join(root, filename))
19+
20+
# Create main translation file
21+
print(files)
22+
cmd = ("xgettext -L Python --keyword=tr --keyword=trn:1,2 --keyword=pgettext:1c,2 --from-code=UTF-8 " +
23+
"--flag=tr:1:python-brace-format --flag=trn:1:python-brace-format --flag=trn:2:python-brace-format " +
24+
"-o translations/app.pot {}").format(" ".join(files))
25+
print(cmd)
26+
27+
ret = os.system(cmd)
28+
assert ret == 0
29+
30+
# Generate/update translation files for each language
31+
with open(LANGUAGES_FILE) as f:
32+
translation_files = json.load(f).values()
33+
34+
for file in translation_files:
35+
name = file.replace("main_", "")
36+
if os.path.exists(os.path.join(TRANSLATIONS_DIR, f"app_{name}.po")):
37+
cmd = "msgmerge --update --backup=none --sort-output translations/app.pot translations/app_{}.po".format(name)
38+
ret = os.system(cmd)
39+
assert ret == 0
40+
else:
41+
cmd = "msginit -l es --no-translator --input translations/app.pot --output-file translations/app_{}.po".format(name)
42+
ret = os.system(cmd)
43+
assert ret == 0
44+
45+
46+
if __name__ == "__main__":
47+
update_translations()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4+
cd "$BASEDIR"
5+
pwd
6+
7+
PY_FILES=$(git ls-files 'layouts/*.py' 'widgets/*.py')
8+
9+
xgettext -L Python \
10+
--keyword=tr \
11+
--keyword=trn:1,2 \
12+
--keyword=pgettext:1c,2 \
13+
--from-code=UTF-8 \
14+
--flag=tr:1:python-brace-format \
15+
--flag=trn:1:python-brace-format --flag=trn:2:python-brace-format \
16+
-o translations/app.pot \
17+
$PY_FILES
18+
19+
msginit \
20+
-l es \
21+
--no-translator \
22+
--input translations/app.pot \
23+
--output-file translations/app.po

selfdrive/ui/widgets/prime.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from openpilot.selfdrive.ui.ui_state import ui_state
44
from openpilot.system.ui.lib.application import gui_app, FontWeight
5+
from openpilot.system.ui.lib.multilang import tr
56
from openpilot.system.ui.lib.text_measure import measure_text_cached
67
from openpilot.system.ui.lib.wrap_text import wrap_text
78
from openpilot.system.ui.widgets import Widget
@@ -29,21 +30,21 @@ def _render_for_non_prime_users(self, rect: rl.Rectangle):
2930
w = rect.width - 160
3031

3132
# Title
32-
gui_label(rl.Rectangle(x, y, w, 90), "Upgrade Now", 75, font_weight=FontWeight.BOLD)
33+
gui_label(rl.Rectangle(x, y, w, 90), tr("Upgrade Now"), 75, font_weight=FontWeight.BOLD)
3334

3435
# Description with wrapping
3536
desc_y = y + 140
3637
font = gui_app.font(FontWeight.LIGHT)
37-
wrapped_text = "\n".join(wrap_text(font, "Become a comma prime member at connect.comma.ai", 56, int(w)))
38+
wrapped_text = "\n".join(wrap_text(font, tr("Become a comma prime member at connect.comma.ai"), 56, int(w)))
3839
text_size = measure_text_cached(font, wrapped_text, 56)
3940
rl.draw_text_ex(font, wrapped_text, rl.Vector2(x, desc_y), 56, 0, rl.WHITE)
4041

4142
# Features section
4243
features_y = desc_y + text_size.y + 50
43-
gui_label(rl.Rectangle(x, features_y, w, 50), "PRIME FEATURES:", 41, font_weight=FontWeight.BOLD)
44+
gui_label(rl.Rectangle(x, features_y, w, 50), tr("PRIME FEATURES:"), 41, font_weight=FontWeight.BOLD)
4445

4546
# Feature list
46-
features = ["Remote access", "24/7 LTE connectivity", "1 year of drive storage", "Remote snapshots"]
47+
features = [tr("Remote access"), tr("24/7 LTE connectivity"), tr("1 year of drive storage"), tr("Remote snapshots")]
4748
for i, feature in enumerate(features):
4849
item_y = features_y + 80 + i * 65
4950
gui_label(rl.Rectangle(x, item_y, 100, 60), "✓", 50, color=rl.Color(70, 91, 234, 255))
@@ -58,5 +59,5 @@ def _render_for_prime_user(self, rect: rl.Rectangle):
5859
y = rect.y + 40
5960

6061
font = gui_app.font(FontWeight.BOLD)
61-
rl.draw_text_ex(font, "✓ SUBSCRIBED", rl.Vector2(x, y), 41, 0, rl.Color(134, 255, 78, 255))
62-
rl.draw_text_ex(font, "comma prime", rl.Vector2(x, y + 61), 75, 0, rl.WHITE)
62+
rl.draw_text_ex(font, tr("✓ SUBSCRIBED"), rl.Vector2(x, y), 41, 0, rl.Color(134, 255, 78, 255))
63+
rl.draw_text_ex(font, tr("comma prime"), rl.Vector2(x, y + 61), 75, 0, rl.WHITE)

system/ui/lib/multilang.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def __init__(self):
1313
self._load_languages()
1414
self._hook_draw_text()
1515

16+
def translate(self, text: str) -> str:
17+
if self._language not in self._translations:
18+
return text
19+
return self._translations[self._language].get(text, text)
20+
1621
def _load_languages(self):
1722
self._language = Params().get("LanguageSetting")
1823

@@ -44,3 +49,7 @@ def draw_text_ex_wrapper(font: Union[rl.Font, list, tuple], text: str, position:
4449

4550
rl.draw_text = draw_text_wrapper
4651
rl.draw_text_ex = draw_text_ex_wrapper
52+
53+
54+
multilang = Multilang()
55+
tr = multilang.translate

0 commit comments

Comments
 (0)