问题描述
我可以翻译来自QtDesigner的文本,但是我不能翻译它之外定义的任何内容.
I can transalate texts that comes from QtDesigner, but I can't translate anything that is defined outside it.
在此示例代码中:
from PyQt4.QtCore import QCoreApplication
tr = QCoreApplication.translate
class Flag(object):
def __init__(self, name):
self._name = name
self._setting_events = []
self._clearing_events = []
self._toggle_events = []
self._true_name = tr("Flags", u'True')
self._false_name = tr("Flags", u'False')
根据文档,第一个参数是 context em>,第二个是 sourceText .但是,当我在QtLinguist中打开.ts
文件时,它表明 context 是我的 sourceText 和 sourceText 是注释.无论如何,在QtLinguist中将其翻译后,我释放了.qm
文件并运行了我的应用程序,但是文本没有改变.我只看到通过的 sourceText ,所以在此示例中,它仍然是'True'
而不是我翻译的内容.
According to documentation first parameter is context and second is sourceText. But when I open my .ts
file in QtLinguist, it shows that context is my sourceText and sourceText is a comment. Whatever, after translating it in QtLinguist I release .qm
files and I run my app, but texts does not change. I see only passed sourceText, so in this example it's still 'True'
and not what I translated.
我在做什么错了?
推荐答案
我只是陷入了同一个陷阱.上面piccy的评论就说明了一切.
I just fell in the same trap. piccy's comment above says it all.
pylupdate是仅"文件解析器.它搜索tr()
和translate()
作为字符串.它会忽略诸如my_tr_func = translate
之类的影响.
pylupdate is "just" a file parser. It searches for tr()
and translate()
as strings. It ignores affectations such as my_tr_func = translate
.
如果您写
my_tr_func = translate
text = my_tr_func("Context", "Source text")
您的字符串将被忽略.
这里的窍门是,您使用tr()作为别名,而不仅仅是任何字符串,并且pylupdate将其误认为QObject tr()方法,并相应地解析了其参数,而不是忽略它.
The trick here is that you used tr() as an alias, not just any string, and instead of just ignoring it, pylupdate mistook it for the QObject tr() method and parsed its arguments accordingly.
对此您无能为力(除非您修补pylupdate ...).
There's nothing much you can do against this (unless you patch pylupdate...).
请注意,您显然可以编写
Note that apparently, you can write
translate = QtCore.QCoreApplication.translate
text = translate("Context", "Source text")
总比没有好.
这篇关于PyQt国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!