简体   繁体   English

PyQt5 QTranslator 翻译不适用于常量文件

[英]PyQt5 QTranslator translate not working with constants file

I am trying to learn pyqt and some mvc pattern stuff.我正在尝试学习 pyqt 和一些 mvc 模式的东西。 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:这是我初始化 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在我看来,我有一个函数,它从字典创建菜单,字典本身是来自文件 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:常量.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) (我创建了 ts 文件,在 QT linguist 中对其进行了编辑并构建了 qm 文件)

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.在我的代码中的任何地方,该字符串都被正确翻译此外,如果将 MENU 常量直接放入我的模型中,一切都很好。 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):根据请求,一个简单的工作示例(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): (其他.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上面的constants.py

Ok, seems to be an import problem.好的,似乎是导入问题。 If i import Main (which itselfs import MENU) after Qtranslator has been initialized, everything works.如果我在 Qtranslator 初始化后导入 Main(它本身导入 MENU),一切正常。 So it seems like translate() is executed at import.所以似乎 translate() 是在导入时执行的。 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.看起来有点hacky,但似乎有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM