Introducing Qt Resource Internationalization

Here is a quote from the Qt documentation :

Some resources need to be modified based on a user locale, such as translation files or icons. This is done by adding the lang attribute to the qresource tag, specifying a suitable locale string. For instance:

<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 (that is, QLocale :: system (). Name () returns "fr_FR"): / cut.jpg becomes a link to the image cut_fr.jpg. For other locales, cut.jpg is used.

I am trying to do this and I am failing. Here is part of 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 can see, it exactly follows the same pattern as the example in the manual. However, an attempt to compile this file yields the following:

 ..\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 change the * .qrc file in QtCreator, it resets it to the wrong state, removing 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 have to iterate over resources for different locales in my code. Am I missing something or is it a Qt error? Qt version is 4.8.4, QtCreator version is 2.8.1.

+6
source share
1 answer

I don’t know, maybe this will help you. The file from the documentation does not work for me either. 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 the constructor for windows. The designer sees only tr.png (triangle.png). The standard build environment is LANGUAGE = uk . After changing to LANGUAGE = ru in Qt Creator, the program started showing triangle_en.png.

I am using Qt 5.0.2 and Qt Creator 2.8.1.

+1
source

All Articles