Qt/translations

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m
m
Line 33: Line 33:
 
<source lang="bash">
 
<source lang="bash">
 
lrelease myApp.pro
 
lrelease myApp.pro
 +
</source>
 +
 +
==Installing translations==
 +
Load the translation file '''before''' displaying any translated windows:
 +
<source lang="cpp">
 +
if (!translator.load(QString("myApp_") + lang + ".qm")) {
 +
qDebug("!!! translator.load -> FALSE !!! \n");
 +
} else {
 +
app->installTranslator(&translator);
 +
}
 +
 +
main = new MainWindow();
 
</source>
 
</source>

Revision as of 16:36, 11 June 2012

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...");

Providing the translations

First, add each language file to the TRANSLATIONS variable:

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

Installing translations

Load the translation file before displaying any translated windows:

if (!translator.load(QString("myApp_") + lang + ".qm")) {
		qDebug("!!! translator.load -> FALSE !!! \n");
} else {
	app->installTranslator(&translator);
}
 
main = new MainWindow();
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox