PyQt国际化 [英] PyQt internationalization

查看:250
本文介绍了PyQt国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以翻译来自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国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆