<< to CrossControl homepage

Support & Service Center

Usage of several languages / translations in Qt

Printer-friendly versionPrinter-friendly versionPDF versionPDF version

In order to use several languages, the language files have to be added to the project and transfered to the target while deploying. To do this, add the following to your project file (.pro):

TRANSLATIONS += lang_de.ts \
                lang_en.ts
…
tsFile.files = *.qm
tsFile.path = /opt/app-folder
INSTALLS += tsFile

In your application, add the following to the constructor of your Application:

Application::Application(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Application)
{
    ui->setupUi(this);

    QTranslator *translator = new QTranslator();
    if(translator->load(QString("lang_de"), qApp->applicationDirPath())) {

        qApp->installTranslator(translator);
        qDebug() << "Translation file loaded!\n\n";
    }

    ui->retranslateUi(this);
…
…
…
}

The translator-files must not be installed in the main.cpp. The language files can be edited in the QtLinguist tool, which can be installed using

sudo apt-get install qt4-dev-tools

Click here for more information about multi-language support in Qt

whatever