Qt resource internationalization fails

1.2k views Asked by At

Here is the quote from the Qt documentation:

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.

I try to do this and I fail. Here is a part from my *.qrc file:

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

As you see, it follows exactly the same pattern as the example in the manual. However, trying to compile this file yields this:

..\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'

And if I try to modify the *.qrc file in QtCreator, it resets it to a wrong state deleting the lang attributes:

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

So I am forced to iterate through resources for different locales in my code. Am I missing something or is this a Qt bug? Qt version is 4.8.4, QtCreator version is 2.8.1.

1

There are 1 answers

0
dismine On

I don't know maybe this will help you. File from documentation doesn't work for me too. But this work:

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

I used designer for windows. Designer see only tr.png (triangle.png). Default Build Environment is LANGUAGE=uk. After change onto LANGUAGE=en in Qt Creator, program began to show triangle_en.png.

I use Qt 5.0.2 and Qt Creator 2.8.1.