2013-12-16 65 views
6

下面是從Qt documentation報價:Qt的資源國際化失敗

有些資源需要根據用戶的語言環境改變,如 翻譯文件或圖標。這是通過向qresource標籤添加一個lang屬性到 來完成的,指定一個合適的區域設置字符串。例如:

<qresource> 
    <file>cut.jpg</file> 
</qresource> 
<qresource lang="fr"> 
    <file alias="cut.jpg">cut_fr.jpg</file> 
</qresource> 

如果用戶的區域設置爲法語(即QLocale ::系統(的)名稱()返回 「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。

回答

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。