6

这是来自Qt 文档的引用:

一些资源需要根据用户的语言环境进行更改,例如翻译文件或图标。这是通过将 lang 属性添加到 qresource 标记并指定合适的语言环境字符串来完成的。例如:

<qresource>
    <file>cut.jpg</file>
</qresource>
<qresource lang="fr">
    <file alias="cut.jpg">cut_fr.jpg</file>
</qresource>

如果用户的语言环境是法语(即 QLocale::system().name() 返回“fr_FR”),:/cut.jpg 将成为对 cut_fr.jpg 图像的引用。对于其他语言环境,使用 cut.jpg。

我尝试这样做,但我失败了。这是我的 *.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。

4

1 回答 1

2

我不知道也许这会对你有所帮助。文档中的文件也不适用于我。但这项工作:

<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。

于 2013-12-26T22:19:12.480 回答