【问题标题】:Qt resource internationalization failsQt 资源国际化失败
【发布时间】:2014-01-03 21:02:41
【问题描述】:

这是Qt documentation的引用:

有些资源需要根据用户的语言环境而改变,例如 翻译文件或图标。这是通过将 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。

【问题讨论】:

    标签: c++ qt qt4 embedded-resource rcc


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多