Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Optimize Language Loading and Simplify Selection Process #87

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 21 additions & 51 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def __init__(self):
base_ui.CommunicationHandler.__init__(self)

self.profile_ui = profile_ui.ProfileUI(main=self) # load profile without UI
#self.translator = PyQt6.QtCore.QTranslator(self) # Languages must be created before UI loaded
#self.load_language() # load manually
self.load_language_id(self.profile_ui.get_global_setting("language",DEFAULTLANG)) # load language file

base_ui.WidgetUI.__init__(self, None, "MainWindow.ui")

Expand All @@ -93,7 +92,6 @@ def __init__(self):
self.systray : SystrayWrapper = None

self.lang_actions = {}
self.translator = PyQt6.QtCore.QTranslator(self)
self.language_action_group = QActionGroup(self)
self.language_action_group.setExclusive(True)

Expand Down Expand Up @@ -201,42 +199,24 @@ def setup(self):
nb_device_compat = self.serialchooser.get_ports()
self.serialchooser.auto_connect(nb_device_compat)

# TODO this function is likely not required. Remove if language changing is fixed
def load_language(self):
"""load language file in profile befor creating UI"""
app.removeTranslator(self.translator)

langid = self.profile_ui.get_global_setting("language",DEFAULTLANG)
# print(f"Loading langid: {langid}")
if langid == DEFAULTLANG:
langfile = ''
else:
def load_language_id(self, langid:str):
"""load language file"""
if langid != DEFAULTLANG:
langfile = helper.res_path(f"{langid}.qm","translations")
# print(f"Loading language file: {langfile}")
if self.translator.load(langfile):
app.installTranslator(self.translator)
# print(f"Language file loaded: {langfile}")
if translator.load(langfile):
app.installTranslator(translator)

def change_language(self,enabled):
def change_lang_callback(self, enabled:bool):
"""Change language of the UI, this will run too when initializing the UI"""
if(not enabled):
return
langfile,user_language = self.language_action_group.checkedAction().data()

# try to load the user's language file
curlang = self.translator.language()
if self.translator.isEmpty():
curlang = DEFAULTLANG
if(curlang == user_language):
if(not enabled): # Language not selected
return

app.removeTranslator(self.translator)
if self.translator.load(langfile):
app.installTranslator(self.translator)
# print(f"Language file loaded: {langfile}")
self.profile_ui.set_global_setting("language",user_language) #store language

app.removeTranslator(translator)

user_lang_id = self.language_action_group.checkedAction().data()
self.profile_ui.set_global_setting("language",user_lang_id) # store language setting

self.languagechanged.emit()
self.languagechanged.emit() # loading in next start

def restart_app(self):
self.restart_app_flag = True
Expand All @@ -245,30 +225,19 @@ def restart_app(self):
app.quit()

def make_lang_selector(self):
'''Create the language selector menu, and connect the callback to change language.'''
languages = [DEFAULTLANG]
languages.extend([os.path.splitext(os.path.basename(f))[0] for f in glob.glob(helper.res_path("*.qm","translations"))])

languages = [("",DEFAULTLANG)]
languages.extend([(f,os.path.splitext(os.path.basename(f))[0]) for f in glob.glob(helper.res_path("*.qm","translations"))])

for langfile,langid in languages:
for langid in languages:
action = QAction(langid)
action.setData([langfile,langid])
action.setData(langid)
action.setCheckable(True)
self.language_action_group.addAction(action)
self.lang_actions[langid] = action
self.menuLanguage.addAction(action)
action.toggled.connect(self.change_language)

user_language = self.profile_ui.get_global_setting("language",None)

if not user_language:
user_language = PyQt6.QtCore.QLocale.system().name()
action.toggled.connect(self.change_lang_callback)

if user_language in self.lang_actions:
self.lang_actions.get(user_language).setChecked(True)
else:
self.lang_actions.get(DEFAULTLANG).setChecked(True)



def reboot(self):
"""Send the reboot message to the board."""
Expand Down Expand Up @@ -917,6 +886,7 @@ def process_events():
restart = True
exit_code = -1
app = PyQt6.QtWidgets.QApplication(sys.argv)
translator = PyQt6.QtCore.QTranslator(app) # Translator must be created before UI loaded
while(restart):
restart = False
window = MainUi()
Expand Down
1 change: 0 additions & 1 deletion profile_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def __init__(self, main=None):

self.ui_initialized = False

# 加载配置文件和资料
self.load_profile_settings()
self.load_profiles()

Expand Down
Loading