I am trying to learn pyqt and some mvc pattern stuff. I'd also like to have some translation for my app. Basic experiments worked great, so i tried to implement it within my app, but here i struggle right now.
here is where i initialize QTranslator:
import sys
from PyQt5.QtCore import QTranslator
from PyQt5.QtWidgets import QApplication
from controller.main_controller import MainController
if __name__ == '__main__':
KodiDBEditor = QApplication(sys.argv)
translator = QTranslator(KodiDBEditor)
translator.load("translate/de_DE.qm")
KodiDBEditor.installTranslator(translator)
main = MainController()
sys.exit(KodiDBEditor.exec_())
My Controller initializes the view, collecting data from the model:
**snip**
def init_ui(self):
self.view.setWindowTitle(self.model.get_title())
self.view.setGeometry(self.model.get_geometry())
self.view.create_menus(self.model.get_menu())
Within my view, i have a function, which creates menus from a dictionary, which itself is a constant from a file constants.py
**snip**
def create_menus(self, menu):
for i in menu:
fm = self.menuBar().addMenu(i['label'])
for j in i['action']:
if j == '---':
fm.addSeparator()
else:
action = QAction()
action.setParent(self)
action.setText(j['label'])
action.setIcon(QIcon.fromTheme(j['icon']))
action.setShortcut(j['shortcut'])
action.setStatusTip(j['statustip'])
fm.addAction(action)
constants.py:
from PyQt5.QtCore import QCoreApplication
translate = QCoreApplication.translate
MENU = (
{'label': translate('MainModel', '&File'), 'action': (
{'label': translate('MainModel','Export'), 'icon': '', 'shortcut': 'Ctrl+B', 'statustip': translate('MainModel','Export Database')},
'---',
{'label': 'E&xit', 'icon': 'application-exit', 'shortcut': 'Ctrl+Q', 'statustip': 'Exit Application'}
)},
{'label': '&Settings', 'action': (
{'label': 'KodiDBEditor', 'icon': '', 'shortcut': '', 'statustip': 'Einstellungen für KodiDBEditor'},
)},
{'label': '&Hilfe', 'action': (
{'label': 'Sub1', 'icon': '', 'shortcut': '', 'statustip': 'Sub1'},
{'label': 'Sub2', 'icon': '', 'shortcut': '', 'statustip': 'Sub2'},
{'label': 'Sub3', 'icon': '', 'shortcut': '', 'statustip': 'Sub3'}
)}
)
However, the parts in question won't be translated. (I created the ts file, edited it in QT linguist and built the qm file)
If i place some single code like:
print(translate('MainModel', '&File'))
anywhere in my code, that string is translated correctly Also, if place the MENU constant straight into my model, everything is fine. However, i prefer to have the separate constants approach (since there will be more)
So the question is, why doesn't my code translate as it is?
As per request, a simple working example (minimal.py):
import sys
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtCore import QTranslator
from PyQt5.QtWidgets import QApplication
from other import Main
translate = QCoreApplication.translate
if __name__ == '__main__':
KodiDBEditor = QApplication(sys.argv)
translator = QTranslator(KodiDBEditor)
translator.load("translate/de_DE.qm")
KodiDBEditor.installTranslator(translator)
main = Main()
sys.exit(KodiDBEditor.exec_())
(other.py):
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import QMainWindow
from misc.constants import MENU
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.printstuff()
self.show()
def printstuff(self):
translate = QCoreApplication.translate
MENU2 = (
{'label': translate('MainModel', '&File'), 'action': (
{'label': translate('MainModel', 'Export'), 'icon': '', 'shortcut': 'Ctrl+B',
'statustip': translate('MainModel', 'Export Database')},
'---',
{'label': 'E&xit', 'icon': 'application-exit', 'shortcut': 'Ctrl+Q', 'statustip': 'Exit Application'}
)},
{'label': '&Settings', 'action': (
{'label': 'KodiDBEditor', 'icon': '', 'shortcut': '', 'statustip': 'Einstellungen für KodiDBEditor'},
)},
{'label': '&Hilfe', 'action': (
{'label': 'Sub1', 'icon': '', 'shortcut': '', 'statustip': 'Sub1'},
{'label': 'Sub2', 'icon': '', 'shortcut': '', 'statustip': 'Sub2'},
{'label': 'Sub3', 'icon': '', 'shortcut': '', 'statustip': 'Sub3'}
)})
print(MENU) # tuple holding the menu dicts. strings are not translated, &File = &File
print(translate('MainModel', '&File')) # this is working &File = &Datei
print(MENU2) #this is working as well
constants.py as above
Ok, seems to be an import problem. If i import Main (which itselfs import MENU) after Qtranslator has been initialized, everything works. So it seems like translate() is executed at import. Is there a way to avoid that? Right now my workaround is to call translate a second time, when the action text is set. Looks a bit hacky, but seems to work.