From 25e362c338dbdc4de61817f60074126e08630997 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 20 Nov 2018 14:11:18 +0200 Subject: [PATCH] Removing RetranslateMixin from MainWindow --- src/bitmessageqt/__init__.py | 5 +-- src/bitmessageqt/address_dialogs.py | 9 ++-- src/bitmessageqt/main.py | 69 ++++++++++++++++++++++++++++- src/bitmessageqt/networkstatus.py | 2 +- 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index e3fb58fe..69578bda 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -308,7 +308,6 @@ class MainWindow(Window): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) - self.qmytranslator = self.qsystranslator = None self.indicatorUpdate = None self.actionStatus = None @@ -1269,8 +1268,8 @@ class MainWindow(Window): def changeEvent(self, event): if event.type() == QtCore.QEvent.LanguageChange: - self.retranslateUi(self) - self.init_inbox_popup_menu(False) + # FIXME: it's called very often + self.retranslateUi() self.init_identities_popup_menu(False) self.blackwhitelist.init_blacklist_popup_menu() if event.type() == QtCore.QEvent.WindowStateChange: diff --git a/src/bitmessageqt/address_dialogs.py b/src/bitmessageqt/address_dialogs.py index 56cf7cc5..a5755013 100644 --- a/src/bitmessageqt/address_dialogs.py +++ b/src/bitmessageqt/address_dialogs.py @@ -1,7 +1,7 @@ """ Dialogs that work with BM address. """ -# pylint: disable=attribute-defined-outside-init,too-few-public-methods,relative-import +# pylint: disable=attribute-defined-outside-init,too-few-public-methods import hashlib @@ -9,8 +9,11 @@ from PyQt4 import QtCore, QtGui import queues import widgets -from account import AccountMixin, GatewayAccount, MailchuckAccount, accountClass, getSortedAccounts -from addresses import addBMIfNotPresent, decodeAddress, encodeVarint +from account import ( + GatewayAccount, MailchuckAccount, AccountMixin, accountClass, + getSortedAccounts +) +from addresses import decodeAddress, encodeVarint, addBMIfNotPresent from inventory import Inventory from tr import _translate diff --git a/src/bitmessageqt/main.py b/src/bitmessageqt/main.py index 6aab8be4..205342e8 100644 --- a/src/bitmessageqt/main.py +++ b/src/bitmessageqt/main.py @@ -1,20 +1,26 @@ +import locale +import os +import sys from PyQt4 import QtCore, QtGui +import l10n +import paths import settingsmixin import widgets from bmconfigparser import BMConfigParser +from debug import logger from foldertree import AddressBookCompleter -from retranslateui import RetranslateMixin from tr import _translate -class Window(settingsmixin.SMainWindow, RetranslateMixin): +class Window(settingsmixin.SMainWindow): """The main PyBitmessage's window""" def __init__(self, parent=None): super(Window, self).__init__(parent) widgets.load('main.ui', self) + self.qmytranslator = self.qsystranslator = None self.blackwhitelist.rerenderBlackWhiteList() addressBookCompleter = AddressBookCompleter() @@ -82,3 +88,62 @@ class Window(settingsmixin.SMainWindow, RetranslateMixin): if dontconnect else _translate("MainWindow", "Go offline", None) ) + + def retranslateUi(self): + """Update widgets' texts which is not taken from ui-file""" + self.updateHumanFriendlyTTLDescription(int( + self.horizontalSliderTTL.tickPosition() ** 3.199 + 3600)) + self.networkstatus.retranslateUi() + + # FIXME: this is not best place for this func + def change_translation(self, newlocale=None): + """Change translation language for the application""" + if newlocale is None: + newlocale = l10n.getTranslationLanguage() + try: + if not self.qmytranslator.isEmpty(): + QtGui.QApplication.removeTranslator(self.qmytranslator) + except: + pass + try: + if not self.qsystranslator.isEmpty(): + QtGui.QApplication.removeTranslator(self.qsystranslator) + except: + pass + + self.qmytranslator = QtCore.QTranslator() + translationpath = os.path.join( + paths.codePath(), 'translations', 'bitmessage_' + newlocale) + self.qmytranslator.load(translationpath) + QtGui.QApplication.installTranslator(self.qmytranslator) + + self.qsystranslator = QtCore.QTranslator() + if paths.frozen: + translationpath = os.path.join( + paths.codePath(), 'translations', 'qt_' + newlocale) + else: + translationpath = os.path.join( + str(QtCore.QLibraryInfo.location( + QtCore.QLibraryInfo.TranslationsPath)), 'qt_' + newlocale) + self.qsystranslator.load(translationpath) + QtGui.QApplication.installTranslator(self.qsystranslator) + + lang = locale.normalize(l10n.getTranslationLanguage()) + langs = [ + lang.split(".")[0] + "." + l10n.encoding, + lang.split(".")[0] + "." + 'UTF-8', + lang + ] + if 'win32' in sys.platform or 'win64' in sys.platform: + langs = [l10n.getWindowsLocale(lang)] + for lang in langs: + try: + l10n.setlocale(locale.LC_ALL, lang) + if 'win32' not in sys.platform and 'win64' not in sys.platform: + l10n.encoding = locale.nl_langinfo(locale.CODESET) + else: + l10n.encoding = locale.getlocale()[1] + logger.info("Successfully set locale to %s", lang) + break + except: + logger.error("Failed to set locale to %s", lang, exc_info=True) diff --git a/src/bitmessageqt/networkstatus.py b/src/bitmessageqt/networkstatus.py index e7fd9e94..4abda307 100644 --- a/src/bitmessageqt/networkstatus.py +++ b/src/bitmessageqt/networkstatus.py @@ -240,7 +240,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): self.labelTotalConnections.setText( _translate( "networkstatus", "Total Connections: %1").arg( - str(self.tableWidgetConnectionCount.rowCount()))) + self.tableWidgetConnectionCount.rowCount())) self.labelStartupTime.setText(_translate( "networkstatus", "Since startup on %1" ).arg(l10n.formatTimestamp(self.startup)))