使用 gettext 而不是 QObject.tr() 来本地化 PyQt4 应用程序的优点和缺点?
我有几个用 PyQt4 编写的应用程序,其中我使用标准 Python gettext 库进行 GUI 的国际化和本地化。这对我来说很有用。但我选择 gettext 只是因为我已经拥有 gettext 使用的知识和经验,并且对 Qt4 tr() 方法的经验为零。
现在我想更好地比较这两种方法,并了解使用 gettext 而不是 QObject.tr 所缺少的东西,并且是否有任何严重的原因为什么我不应该对 Qt4/PyQt4 应用程序使用 gettext?
根据我的理解,使用 gettext 的优点是:
- GNU gettext 很成熟,它似乎是 GNU/Linux 世界中事实上的标准。
- 尽管 PO 模板的文本性质使其并非绝对必要,但 PO 文件有足够的专用编辑器来简化翻译人员的工作。
- 甚至还有可用于协作翻译的网络服务。
- gettext 是标准的 Python 库,所以我不需要安装任何特殊的东西就可以在运行时使用它。
- 它对通过 ngettext() 选择单数/复数形式有很好的支持。
我认为 QObject.tr() 的优点:
- 这是 Qt4/PyQt4 的原生技术,所以也许它会工作得更好/更快(尽管我没有数据可以证明)。
- 要翻译的消息可能具有附加的上下文信息,这将帮助翻译人员选择同音词的最佳变体,例如,英语单词“信件”可以翻译为“字符”、“邮件”,甚至可以翻译为“纸张尺寸”,具体取决于实际情况。
我认为 QObject.tr() 与 gettext 的缺点:
- 我没有在 Qt 文档中找到如何支持单数/复数选择。
- Qt4 TS 翻译模板是 XML 格式,因此在没有特殊编辑器(QT Linguist)的情况下编辑起来更加复杂,并且似乎没有其他第三方解决方案或 Web 服务。因此,翻译人员需要学习新的工具(如果他们已经熟悉 PO 工具)。
但上述所有项目都不足以明确地说任何工具都优于其他工具。我不想就什么更好展开激烈的争论,因为这是非常主观的。我只是想知道 QObject.tr() 与 gettext 的优缺点。
I have couple of application written in PyQt4 where I've used standard Python gettext library for internationalization and localization of GUI. It works good for me. But I've selected gettext just because I've already had knowledge and experience of gettext usage, and zero of experience with Qt4 tr() approach.
Now I'd like to better compare both approaches and understand what I'm missing by using gettext instead of QObject.tr, and does there any serious reason why I should not use gettext for Qt4/PyQt4 applications?
In my understanding advantages of using gettext are:
- GNU gettext is mature and it seems to be standard de-facto in GNU/Linux world.
- There is enough special editors for PO files to simplify translators work, although textual nature of PO templates makes it not strictly necessary.
- There is even web services available which can be used for collaborative translations.
- gettext is standard Python library, so I don't need to install anything special to use it in runtime.
- It has very good support for singular/plural forms selection via ngettext().
What I see as advantages of QObject.tr():
- This is native technology for Qt4/PyQt4 so maybe it will work better/faster (although I have no data to prove).
- The messages to translate may have additional context information which will help translators to choose the best variants for homonym words, e.g. the english word "Letter" can be translates as "Character", "Mail" or even kind of "Paper size" depending on the actual context.
What I see as disadvantages of QObject.tr() vs gettext:
- I did not found in the Qt documentation how's supported singular/plural selection there.
- Qt4 TS translation template is in XML format and therefore more complex to edit without special editor (QT Linguist) and it seems there is no other third-party solutions or web services. So it would require for translators to learn new tool (if they are already familiar with PO tools).
But all the items above are not critical enough to clearly say that any tool is better of other. And I don't want to start flame war about what is better because it's very subjective. I just want to know what I missing as pros and cons of QObject.tr() vs gettext.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 QObject.tr() 的一个简单原因是:
它使您无需在 Windows 上安装 gettext,从而使跨平台工作变得更加容易。
我尝试在 Windows 上尽可能减少二进制依赖性。
One simple reason to use
QObject.tr()
is:It saves you the need to install gettext on Windows, making cross-platform work a bit easier.
I try to have as little binary dependencies as possible on Windows.
所有这些都有其优点和缺点,但为了更清楚地定义它们,您必须首先定义您的目标是移动环境还是桌面环境。
在我们公司内部,我们使用不同的方法只是因为尚不存在理想的解决方案。
对于桌面开发,我们使用 PO 文件只是因为按钮没有缩放,因此文本会适合。
对于移动开发,字符串的翻译取决于按钮的大小,在横向和纵向设备上可能会有所不同。
所以这有点复杂,因为 PO 文件只能有某个单词的 1 个翻译。
因此我们为此选择了 XLIFF,这样我们就可以为字符串分配唯一的 ID。
这也不是一件容易的事,因为没有好的解决方案将 .RC 文件转换为 XLIFF 文件。
(因为当前的工具将所有字符串转换为“”,这当然是不需要的行为)。
所以我为此任务编写了一个转换器。
然而,在考虑本地化时,复数形式非常重要,因此没有它并不是一个好的本地化解决方案。
因此,我建议使用 PO gettext。
问候,
弗洛里斯。
All have their pros and cons, but to define them more clearly you would have to define first if you're targeting a mobile environment or a desktop environment.
Within our company we use different methods simply because the ideal solution does not exist yet.
For desktop development we're using PO files simply because the buttons are not scaled and therefore text will fit.
For mobile development, the translation of a string depends on the button size which could be different on landscape and portrait devices.
So this complicates it a little because a PO file can just have 1 translation of a certain word.
So we selected XLIFF for this, so we could assign unique ID's to a string.
This is not an easy task as well, because there are no good solutions to convert .RC files to XLIFF files.
(Because current tools convert ALL strings between "" which is of course unwanted behavior).
So I wrote a converter for this task.
However, when thinking of localization, then plural forms are very important so not having this is not a good localization solution.
Therefore, I would say to go for PO gettext.
Greetings,
Floris.
目前,当您使用 QT_TRANSLATE_NOOP 时,Qt 不处理复数形式
At the current time, Qt does not handle plural forms when you're making use of QT_TRANSLATE_NOOP
您可以补充说,参数的管理方式不同......
使用 Gettext,我们可以执行
_("Hello %(name)s from %(city)s") % {person.__dict__}
而在 PyQt 中,我们执行
self.tr("Hello %1 from %2").arg(person.name).arg(person.city)
You could add that args are managed differently...
With Gettext, we can do
_("Hello %(name)s from %(city)s") % {person.__dict__}
whereas in PyQt, we do
self.tr("Hello %1 from %2").arg(person.name).arg(person.city)