Qt/translations
From Attie's Wiki
(Difference between revisions)
m |
m (→Providing the translations) |
||
(8 intermediate revisions by one user not shown) | |||
Line 5: | Line 5: | ||
</source> | </source> | ||
− | and wrap any strings with the <code> | + | and wrap any strings with the <code>QApplication::translate()</code> or the <code>QObject::tr()</code> function: |
<source lang="cpp"> | <source lang="cpp"> | ||
− | QString | + | QString message = app->translate("Message displayed while the system is loading", "Loading, Please Wait..."); |
− | + | ||
− | + | ||
− | + | ||
</source> | </source> | ||
==Providing the translations== | ==Providing the translations== | ||
− | First, add each language file to the <code>TRANSLATIONS</code> variable: | + | First, add each language file to the <code>TRANSLATIONS</code> variable in your <code>*.pro</code> file: |
<source lang="text"> | <source lang="text"> | ||
TRANSLATIONS = myApp_en.ts \ | TRANSLATIONS = myApp_en.ts \ | ||
myApp_fr.ts | myApp_fr.ts | ||
</source> | </source> | ||
− | |||
Run <code>lupdate</code> on the project: | Run <code>lupdate</code> on the project: | ||
Line 25: | Line 21: | ||
lupdate myApp.pro | lupdate myApp.pro | ||
</source> | </source> | ||
− | |||
Provide the translations: | Provide the translations: | ||
Line 31: | Line 26: | ||
linguist *.ts | linguist *.ts | ||
</source> | </source> | ||
− | |||
Release the translations: | Release the translations: | ||
<source lang="bash"> | <source lang="bash"> | ||
lrelease myApp.pro | lrelease myApp.pro | ||
+ | </source> | ||
+ | |||
+ | ==Installing translations== | ||
+ | Load the translation file ''before'' loading or displaying any translated windows: | ||
+ | <source lang="cpp"> | ||
+ | QApplication app(argc, argv); | ||
+ | QTranslator translator; | ||
+ | MainWindow *main; | ||
+ | |||
+ | if (!translator.load(QString("myApp_") + lang + ".qm")) { | ||
+ | qDebug("!!! translator.load -> FALSE!!! \n"); | ||
+ | } else { | ||
+ | app->installTranslator(&translator); | ||
+ | } | ||
+ | |||
+ | /* it's important that you do things like this AFTER installing the translator */ | ||
+ | main = new MainWindow(); | ||
+ | </source> | ||
+ | |||
+ | ==Running== | ||
+ | Don't forget to copy the <code>*.qm</code> files into the same directory as the application! | ||
+ | <source lang="bash"> | ||
+ | cp ../myApp/*.qm . | ||
</source> | </source> |
Latest revision as of 17:57, 4 September 2012
Contents |
[edit] Indicating what to translate
Include QTranslator:
#include <QTranslator>
and wrap any strings with the QApplication::translate()
or the QObject::tr()
function:
QString message = app->translate("Message displayed while the system is loading", "Loading, Please Wait...");
[edit] Providing the translations
First, add each language file to the TRANSLATIONS
variable in your *.pro
file:
TRANSLATIONS = myApp_en.ts \ myApp_fr.ts
Run lupdate
on the project:
lupdate myApp.pro
Provide the translations:
linguist *.ts
Release the translations:
lrelease myApp.pro
[edit] Installing translations
Load the translation file before loading or displaying any translated windows:
QApplication app(argc, argv); QTranslator translator; MainWindow *main; if (!translator.load(QString("myApp_") + lang + ".qm")) { qDebug("!!! translator.load -> FALSE!!! \n"); } else { app->installTranslator(&translator); } /* it's important that you do things like this AFTER installing the translator */ main = new MainWindow();
[edit] Running
Don't forget to copy the *.qm
files into the same directory as the application!
cp ../myApp/*.qm .