Qt资源国际化失败

6

这里是来自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。

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版Designer。Designer只看到tr.png(即triangle.png)。默认的构建环境为LANGUAGE=uk。在Qt Creator中将其改为LANGUAGE=en后,程序开始显示triangle_en.png

我使用的是Qt 5.0.2和Qt Creator 2.8.1。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接