From 2d21de0e43d798f277209ff7dead81002bec1f75 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Wed, 2 Feb 2022 20:04:50 +0100 Subject: [PATCH] make language changeable --- aboutdialog.py | 2 +- main.py | 20 ++------------------ raven/i18n.py | 4 ++++ raven/plugins/ravenlogplugin.py | 20 ++++++++++++++++++++ settings.py | 6 +++--- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/aboutdialog.py b/aboutdialog.py index c0dd8e1..bde1cd7 100644 --- a/aboutdialog.py +++ b/aboutdialog.py @@ -65,7 +65,7 @@ class AboutDialog(QDialog): """.format(pyside=PySide6.__version__, qt=PySide6.QtCore.__version__) label = _(textwrap.dedent(dependencies)) diff --git a/main.py b/main.py index 62cc6c7..1a1028c 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,8 @@ from ravenui import RavenUI import gettext +from settingsstore import SettingsStore + gettext.install('ravenlog', 'locale') logging.basicConfig(level=logging.INFO) @@ -43,22 +45,6 @@ if __name__ == "__main__": myappid = 'opentext.ravenlog' # arbitrary string ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) - locale = os.environ['LANG'] if os.environ['LANG'] else "en" - - src_dir = Path(__file__).resolve().parent - try: - translation = gettext.translation('messages', localedir=src_dir / 'locales', languages=[locale]) - if translation: - translation.install() - _ = translation.gettext - ngettext = translation.ngettext - except FileNotFoundError: - pass - if not _: - _ = gettext.gettext - ngettext = gettext.ngettext - print('No translation found') - # workaround to make signals work in QT apps. # They do not work out of the box, because the main thread # is running in C++ code once app.exec() is executed @@ -74,8 +60,6 @@ if __name__ == "__main__": PluginRegistry.load_plugin("LogFilePlugin") PluginRegistry.load_plugin("NotesPlugin") - PluginRegistry.execute("set_translator", lambda string: translator.tr(string)) - window = PluginRegistry.execute_single("create_main_window") RavenUI.window = window window.show() diff --git a/raven/i18n.py b/raven/i18n.py index d195c95..9c53511 100644 --- a/raven/i18n.py +++ b/raven/i18n.py @@ -2,7 +2,11 @@ import gettext import os from pathlib import Path +from settingsstore import SettingsStore + +settings = SettingsStore.load() locale = os.environ['LANG'] if os.environ['LANG'] else "en" +locale = settings.session.get('general', 'lang', fallback=locale) _ = False src_dir = Path(__file__).resolve().parent.parent diff --git a/raven/plugins/ravenlogplugin.py b/raven/plugins/ravenlogplugin.py index 4cdb377..33b1439 100644 --- a/raven/plugins/ravenlogplugin.py +++ b/raven/plugins/ravenlogplugin.py @@ -10,14 +10,20 @@ from raven.mainwindow import MainWindow from raven.pluginbase import PluginBase from raven.plugins.domain.menucontribution import MenuContribution from raven.plugins.domain.raction import RAction +from raven.plugins.domain.rmenu import RMenu from raven.plugins.ravenlog.Tab import Tab from raven.i18n import _ +from settings import Settings + class RavenLogPlugin(PluginBase): def __init__(self): super(RavenLogPlugin, self).__init__() self.main_window = None + def set_settings(self, settings: Settings): + self.settings = settings + def create_main_window(self): if not self.main_window: self.main_window = MainWindow() @@ -27,8 +33,22 @@ class RavenLogPlugin(PluginBase): return [ MenuContribution("file", action=self._action_close(), action_id="close application", after=""), MenuContribution("help", action=self._action_about(), action_id="open about dialog", after=""), + MenuContribution("settings", menu=self._sub_menu_languages(), action_id="recent files menu"), ] + def _sub_menu_languages(self) -> RMenu: + menu = RMenu(_("&Languages")) + menu.add_action(RAction(_("&Default"), lambda: self._set_lang(''))) + menu.add_action(RAction(_("&English"), lambda: self._set_lang('en'))) + menu.add_action(RAction(_("&German"), lambda: self._set_lang('de'))) + return menu + + def _set_lang(self, lang: str): + if (lang == ''): + self.settings.session.remove_option('general', 'lang') + else: + self.settings.session.set('general', 'lang', lang) + def current_file(self) -> Optional[str]: return self.main_window.tabs.current_file() diff --git a/settings.py b/settings.py index bae5dbc..e65df80 100644 --- a/settings.py +++ b/settings.py @@ -9,13 +9,13 @@ class Settings(): def set_session(self, section: str, option: str, value: str): return self.session.set(section, option, value) - def get_session(self, section: str, option: str): + def get_session(self, section: str, option: str) -> str: return self.session.get(section, option) - def getint_session(self, section: str, option: str): + def getint_session(self, section: str, option: str) -> int: return self.session.getint(section, option) - def getboolean_session(self, section: str, option: str): + def getboolean_session(self, section: str, option: str) -> bool: return self.session.getboolean(section, option) @staticmethod