mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-18 07:42:08 +08:00
4ccafff123
* fix multilang dialog height
* split to file
* stash
* Revert "stash"
This reverts commit deb4239fe69f0260420fad03f2350e622e31542f.
* add updater
* add files
* stuff
* try
rev
* stash
* works!
* works!
* this should be the flow?
* cursor wrapping -- it missed entire sections, changed formatting, and didn't use trn properly!!!!!!!!!!!!!!!!!
* update translations
* learned my lesson
* this should be the one thing it's good at
* update trans
* onroad wrap
* spanish
* rename
* clean up
* load all
* Revert "load all"
This reverts commit 6f2a45861c914ffb9d40a5edd15751afd798d614.
* jp translations
* try jp
* Revert "try jp"
This reverts commit d0524b10110104baafcdc1ec385c3d57bc5ef901.
* remove languages we can't add rn
* tr
* pt and fr
* ai cannot be trusted
* ai cannot be trusted
* missing trans
* add fonts
* Revert "remove languages we can't add rn"
This reverts commit 73dc75fae2b9e347d867b6636dab6e2b5fe59da7.
* painfully slow to startup
* only load what we need
* Reapply "remove languages we can't add rn"
This reverts commit 52cb48f3b838520a421f9b90e5ea4409c27d4bd0.
* add system
* that's sick that this just works (dynamic)
* fix description falling back to first str + support callable titles in list items
* device is now live!
* make firehose live
* developer
* network live
* software live
* and that
* toggles live
* regen
* start to clean up gpt
* revert op sans
* bruh
* update translations
* rm old script
* add noops for descriptions to fix translating away from non-english after startup
* missing de
* do filtering in multilang.py
* clean up
clean up
* codespell: ignore po
* fix update
* should not depend
* more live
* sidebar and offroad alert panel live
* fix issues with offroad alerts
* fix firehose live
* fix weird tr("") behavior
* sh key live bugfix
* setup.py live
* update
* update
* no fuzzy matching -- breaks dynamic translations
* rm this
* fix calib desc live trans
* change onroad
* rm dfonts
* clean up device
* missing live
* update
* op lint
* not true
* add to gitignore
* speed up startup by reducing chars by ~half
* fix scons
* fix crash going from qt
* preserve original lang
* cancel kb live translate
* no preserve
* fix lint
77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
import os
|
|
import json
|
|
import gettext
|
|
from openpilot.common.params import Params
|
|
from openpilot.common.basedir import BASEDIR
|
|
|
|
SYSTEM_UI_DIR = os.path.join(BASEDIR, "system", "ui")
|
|
UI_DIR = os.path.join(BASEDIR, "selfdrive", "ui")
|
|
TRANSLATIONS_DIR = os.path.join(UI_DIR, "translations")
|
|
LANGUAGES_FILE = os.path.join(TRANSLATIONS_DIR, "languages.json")
|
|
|
|
SUPPORTED_LANGUAGES = [
|
|
"en",
|
|
"de",
|
|
"fr",
|
|
"pt-BR",
|
|
"es",
|
|
"tr",
|
|
]
|
|
|
|
|
|
class Multilang:
|
|
def __init__(self):
|
|
self._params = Params()
|
|
self.languages = {}
|
|
self.codes = {}
|
|
self._translation: gettext.NullTranslations | gettext.GNUTranslations = gettext.NullTranslations()
|
|
self._load_languages()
|
|
|
|
@property
|
|
def language(self) -> str:
|
|
lang = str(self._params.get("LanguageSetting")).strip("main_")
|
|
if lang not in SUPPORTED_LANGUAGES:
|
|
lang = "en"
|
|
return lang
|
|
|
|
def setup(self):
|
|
language = self.language
|
|
try:
|
|
with open(os.path.join(TRANSLATIONS_DIR, f'app_{language}.mo'), 'rb') as fh:
|
|
translation = gettext.GNUTranslations(fh)
|
|
translation.install()
|
|
self._translation = translation
|
|
print(f"Loaded translations for language: {language}")
|
|
except FileNotFoundError:
|
|
print(f"No translation file found for language: {language}, using default.")
|
|
gettext.install('app')
|
|
self._translation = gettext.NullTranslations()
|
|
return None
|
|
|
|
def change_language(self, language_code: str) -> None:
|
|
# Reinstall gettext with the selected language
|
|
self._params.put("LanguageSetting", language_code)
|
|
self.setup()
|
|
|
|
def tr(self, text: str) -> str:
|
|
return self._translation.gettext(text)
|
|
|
|
def trn(self, singular: str, plural: str, n: int) -> str:
|
|
return self._translation.ngettext(singular, plural, n)
|
|
|
|
def _load_languages(self):
|
|
with open(LANGUAGES_FILE, encoding='utf-8') as f:
|
|
self.languages = {k: v for k, v in json.load(f).items() if v in SUPPORTED_LANGUAGES}
|
|
self.codes = {v: k for k, v in self.languages.items() if v in SUPPORTED_LANGUAGES}
|
|
|
|
|
|
multilang = Multilang()
|
|
multilang.setup()
|
|
|
|
tr, trn = multilang.tr, multilang.trn
|
|
|
|
|
|
# no-op marker for static strings translated later
|
|
def tr_noop(s: str) -> str:
|
|
return s
|