Qt资源国际化失败



这是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.0resources.qrc: Warning: potential duplicate alias detected: 'angle.html'
..Blinky_2.0resources.qrc: Warning: potential duplicate alias detected: 'bottom.html'
..Blinky_2.0resources.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,Qt呼吸机版本为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>

我用设计师设计窗户。设计器只能看到tr.png(triangle.png)。默认的生成环境是LANGUAGE=uk。在Qt Creator中更改为LANGUAGE=en后,程序开始显示triangle_en.png.

我使用Qt 5.0.2和Qt Creator 2.8.1。

最新更新