这是来自 Qt documentation 的引述:
Some resources need to change based on the user's locale, such as translation files or icons. This is done by adding a lang attribute to the qresource tag, specifying a suitable locale string. For example:
<qresource> <file>cut.jpg</file> </qresource> <qresource lang="fr"> <file alias="cut.jpg">cut_fr.jpg</file> </qresource>
If the user's locale is French (i.e., QLocale::system().name() returns "fr_FR"), :/cut.jpg becomes a reference to the cut_fr.jpg image. For other locales, cut.jpg is used.
我尝试这样做但失败了。这是我的 *.qrc 文件的一部分:
<qresource>
<file>HtmlTemplates/angle.html</file>
<file>HtmlTemplates/bottom.html</file>
<file>HtmlTemplates/top.html</file>
</qresource>
<qresource lang="en">
<file alias="HtmlTemplates/angle.html">HtmlTemplates/en/angle.html</file>
<file alias="HtmlTemplates/bottom.html">HtmlTemplates/en/bottom.html</file>
<file alias="HtmlTemplates/top.html">HtmlTemplates/en/top.html</file>
</qresource>
如您所见,它遵循与手册中示例完全相同的模式。 但是,尝试编译此文件会产生以下结果:
..\Blinky_2.0\resources.qrc: Warning: potential duplicate alias detected: 'angle.html'
..\Blinky_2.0\resources.qrc: Warning: potential duplicate alias detected: 'bottom.html'
..\Blinky_2.0\resources.qrc: Warning: potential duplicate alias detected: 'top.html'
如果我尝试在 QtCreator 中修改 *.qrc 文件,它会将其重置为错误状态并删除 lang
属性:
<qresource prefix="/">
<file>HtmlTemplates/angle.html</file>
<file>HtmlTemplates/bottom.html</file>
<file>HtmlTemplates/top.html</file>
<file alias="HtmlTemplates/angle.html">HtmlTemplates/en/angle.html</file>
<file alias="HtmlTemplates/bottom.html">HtmlTemplates/en/bottom.html</file>
<file alias="HtmlTemplates/top.html">HtmlTemplates/en/top.html</file>
</qresource>
所以我不得不在我的代码中针对不同的语言环境遍历资源。我错过了什么或者这是一个 Qt 错误吗? Qt版本为4.8.4,QtCreator版本为2.8.1。
最佳答案
我不知道这也许对你有帮助。文档中的文件对我也不起作用。但是这项工作:
<RCC>
<qresource prefix="/" lang="en">
<file alias="tr.png">triangle_en.png</file>
</qresource>
<qresource prefix="/" lang="uk">
<file alias="tr.png">triangle.png</file>
</qresource>
</RCC>
我使用 windows 设计器。设计师只看到 tr.png (triangle.png)。默认构建环境是 LANGUAGE=uk。在Qt Creator中改成LANGUAGE=en后,程序开始显示triangle_en.png。
我使用 Qt 5.0.2 和 Qt Creator 2.8.1。
关于c++ - Qt资源国际化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20611481/