这里是来自Qt文档的引用:
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的一个bug?Qt版本是4.8.4,QtCreator版本是2.8.1。