diff --git a/src/config.py b/src/config.py index f0097680..1417e86d 100644 --- a/src/config.py +++ b/src/config.py @@ -30,10 +30,21 @@ class Config(object): self.backup_frequence = "00:05:00" self.backup_max = 10 + # Languages + self.lang = "" + @classmethod def filename(cls): return os.environ["HOME"] + config_dir + config_file + @classmethod + def languages(cls): + return { + "System": "", + "English": "en", + "French": "fr", + } + def save(self): os.makedirs(os.path.dirname(self.filename), exist_ok=True) with open(self.filename, 'wb') as out_file: diff --git a/src/pamhyr.py b/src/pamhyr.py index 3c02c327..1ca646ac 100755 --- a/src/pamhyr.py +++ b/src/pamhyr.py @@ -16,9 +16,25 @@ def main(): app = QApplication(sys.argv) translator = QTranslator() - lang = locale.getdefaultlocale() - if "fr" not in lang[0]: - translator.load(os.path.dirname(__file__) + "/lang/fr.qm") + + lang_file = "" + if conf.lang == "": + # System language + sys_lang = locale.getdefaultlocale() + if "fr" in sys_lang[0]: + lang_file = os.path.dirname(__file__) + "/lang/fr.qm" + elif conf.lang == "fr": + # French + lang_file = os.path.dirname(__file__) + "/lang/fr.qm" + else: + lang_file = "" + # English default language + + if lang_file != "": + ok = translator.load(lang_file) + if not ok: + print("failed") + app.installTranslator(translator) application = ApplicationWindow(conf=conf) diff --git a/src/view/ASubWindow.py b/src/view/ASubWindow.py index 29ef8c81..a2bd7549 100644 --- a/src/view/ASubWindow.py +++ b/src/view/ASubWindow.py @@ -237,6 +237,17 @@ class ASubWindow(QDialog): """ self.find(QComboBox, name).setCurrentText(item) + def get_combobox_text(self, name:str): + """Get current text of combo box + + Args: + name: The combo box component name + + Returns: + Current text + """ + return self.find(QComboBox, name).currentText() + # Custom dialog def file_dialog(self, select_file=True, callback=lambda x: None): diff --git a/src/view/ConfigureWindow.py b/src/view/ConfigureWindow.py index 4962fa20..551bf3fa 100644 --- a/src/view/ConfigureWindow.py +++ b/src/view/ConfigureWindow.py @@ -11,7 +11,8 @@ from PyQt5.QtCore import ( from PyQt5.QtWidgets import ( QDialogButtonBox, QPushButton, QLineEdit, - QFileDialog, QTableView, QAbstractItemView + QFileDialog, QTableView, QAbstractItemView, + QComboBox, ) @@ -90,6 +91,13 @@ class ConfigureWindow(ASubWindow, ListedSubWindow): self.find(QTableView, "tableView_solver").resizeColumnsToContents() self.connect() + # Language + languages = Config.languages() + for lang in languages: + self.combobox_add_item("comboBox_language", lang) + if self.conf.lang == languages[lang]: + self.set_combobox_text("comboBox_language", lang) + def connect(self): buttons = { "pushButton_solver_add": self.add_solver, @@ -129,6 +137,9 @@ class ConfigureWindow(ASubWindow, ListedSubWindow): self.conf.backup_frequence = self.get_time_edit("timeEdit_backup_frequence") self.conf.backup_max = self.get_spin_box("spinBox_backup_max") + # Language + self.conf.lang = Config.languages()[self.get_combobox_text("comboBox_language")] + self.end() def reject(self): diff --git a/src/view/MainWindow.py b/src/view/MainWindow.py index 222415d6..4b4e7754 100644 --- a/src/view/MainWindow.py +++ b/src/view/MainWindow.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os +from queue import Queue from PyQt5 import QtGui from PyQt5.QtCore import ( @@ -18,6 +19,7 @@ from view.ConfigureWindow import ConfigureWindow from view.NewStudyWindow import NewStudyWindow from view.NetworkWindow import NetworkWindow from view.AboutWindow import AboutWindow +from view.ui.MainWindow import Ui_MainWindow from model.Study import Study @@ -78,7 +80,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow): Returns: Nothing """ - self.ui.findChild(QAction, action).setEnabled(enable) + self.findChild(QAction, action).setEnabled(enable) def init_callback(self): """Connect action to callback function @@ -115,30 +117,13 @@ class ApplicationWindow(QMainWindow, ListedSubWindow): "action_toolBar_sections": lambda: self.open_dummy("Tronçons"), "action_toolBar_frictions": lambda: self.open_dummy("Frottements"), "action_toolBar_building": lambda: self.open_dummy("Ouvrages"), - ## Language - "action_english": lambda: self.set_language(""), - "action_french": lambda: self.set_language("fr"), } for action in actions: - self.ui.findChild(QAction, action)\ - .triggered.connect(actions[action]) + self.findChild(QAction, action)\ + .triggered.connect(actions[action]) + # action.triggered.connect(actions[action]) - def set_language(self, lang): - if lang != "": - translator = QTranslator() - translator.load(os.path.dirname(__file__) + f"/lang/{lang}.qm") - QApplication.instance().installTranslator(translator) - self.trans = translator - else: - QApplication.instance().removeTranslator(self.trans) - - self.retranslateUi() - - def retranslateUi(self): - for action in self.menubar.children(): - if isinstance(action, QAction): - action.setText(self.trans("MainWindow", action.getText())) def changeEvent(self, event): if event.type() == QEvent.LanguageChange: diff --git a/src/view/ui/ConfigureDialog.ui b/src/view/ui/ConfigureDialog.ui index 77a43e43..c54b2b45 100644 --- a/src/view/ui/ConfigureDialog.ui +++ b/src/view/ui/ConfigureDialog.ui @@ -107,7 +107,7 @@ - + Meshing tool @@ -116,7 +116,7 @@ 10 10 - 621 + 611 30 @@ -155,7 +155,7 @@ 10 10 621 - 93 + 61 @@ -198,7 +198,7 @@ - + Backup @@ -208,7 +208,7 @@ 10 10 611 - 125 + 121 @@ -313,6 +313,51 @@ + + + Language + + + + + 11 + 11 + 309 + 16 + + + + + true + + + + Please restart application after language modification + + + + + + 11 + 33 + 191 + 26 + + + + + + + Language + + + + + + + + + diff --git a/src/view/ui/MainWindow.ui b/src/view/ui/MainWindow.ui index 41ce2ca6..d5a2b900 100644 --- a/src/view/ui/MainWindow.ui +++ b/src/view/ui/MainWindow.ui @@ -201,16 +201,6 @@ - - - - - - &Language - - - - @@ -218,7 +208,6 @@ -