Qt 翻译非源文件中的字符串
我有一个使用 XML 文件的 Qt 项目。这些 XML 文件包含人类可读的文本,并且应使用 Qt 工具(lupdate、lrelease、QtLinguist)翻译该文本。
问题是是否可以通过 lupdate 在 .ts 文件中生成条目,而无需使用 QT_TR_NOOP() 宏和朋友复制源代码文件中 XML 文件中的字符串?或者一般来说,如何翻译 Qt 项目的非源文件中的字符串?
I have a Qt project which uses XML files. Those XML files contain human-readable text and this text should be translated by using the Qt tools (lupdate, lrelease, QtLinguist).
The question is if it is possible to generate entries in .ts file via lupdate without duplicating the strings from the XML files in a source code file by using the QT_TR_NOOP() macro and friends? Or in general, how do you translate strings in non-source files for Qt projects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们遇到了同样的问题:XML 文件包含人类可读的字符串。
我们的解决方案是确保 XML 文件中的人类可读字符串易于提取(我们将它们放在
LABEL
属性中),并且我们开发了一个小工具来解析 XML 文件、提取字符串,生成上下文(通过从 XML 文件中提取数据),然后生成包含QT_TR_NOOP()
列表的 CPP 头文件。该文件已添加到
lupdate
使用的项目文件 (.pro) 中。这个解决方案对我们来说很好,但我们必须非常小心两件事:
We had the same problem : XML files containing human readable strings.
Our solution was to make sure that human readable strings in the XML files were easy to extract (we put them in a
LABEL
attribute) and we developped a small tool which would parse the XML files, extract the strings, generate a context (by extracting data from the XML file), and then generating a CPP header file containing a list ofQT_TR_NOOP()
.This file was added to our project file (.pro) that was used by
lupdate
.This solution was fine for us but we had to be very careful about two things :
只要 .qm 文件具有匹配的翻译/上下文,您就可以使用 tr() 在运行时翻译您想要的任何内容。 lupdate 是否提取它应该没有任何区别。
我不知道如何让 lupdate 从任意 XML 中提取字符串,但这并不意味着您不能使用 linguist。
如果您确实希望将所有内容都放在翻译器的一个文件中,请让您的 XSLT 将 lupdate 生成的文件复制到其输出中。
只要您使用的上下文名称不重复源代码中使用的内容,这与许多应用程序为每个具有 GUI 的 DLL 加载 .qm 的方式应该没有任何不同(从 Qt 的角度来看)。
You can translate anything you want at runtime by using tr(), as long as the .qm file has a matching translation/context. It shouldn't make any difference whether lupdate extracted it or not.
I don't know how to make lupdate to extract strings from arbitrary XML, but that doesn't mean you can't use linguist.
If you really want to have it all in one file for the translator, have your XSLT copy the lupdate-generated file into its output.
As long as you use a context name that doesn't duplicate something used in the source code, this shouldn't be any different (from Qt's point of view) from the way many apps load a .qm for each DLL that has GUI.