Important: Please read the Qt Code of Conduct -

[SOLVED] translator does not install?

  • Hello,
    I am having a very weird problem: I tried the arrowpad example of QT to test QtLinguist. It works fine. I wrote my own little example (even less code than arrowpard) and it didn't work. Here is my code:

    QT       += core gui
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    TARGET = testLinguist
    TEMPLATE = app
    SOURCES += main.cpp\
    TRANSLATIONS = testlinguist_en.ts \
                   testlinguist_de.ts \
    HEADERS  += mainwindow.h
    RESOURCES += \
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        MainWindow w;;
        QString locale = "fr"
        QTranslator translator;
        //bool success = translator.load(QString("testlinguist_") + locale); //did not work just placing the file in the same folder as pro file.
        bool success = translator.load(QString(":\\testlinguist_") + locale);
        return a.exec();
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        QWidget* mainWidget = new QWidget(this);
        QPushButton* button = new QPushButton(tr("&Press"));
        QGridLayout *mainLayout = new QGridLayout(mainWidget);
        mainLayout->addWidget(button, 0, 1);

    at first the success variable in main.cpp was false (I fixed that by putting the qm file in the resources). Now it loads the qm file (success is true), but there still are no translations made. I did everything exactly like in the arrowpad example.
    Does anybody have any idea what could be going wrong here?
    Thanks for your help!

  • Lifetime Qt Champion


    On a side note, please use forward slashes in path, that will make your code cleaner and less error prone.

    Do you only have one text to translate ?

  • this is my test app, I just wrote to test the Linguist. In my real program (where it does not work either) I have of course a lot of text to translate.

  • I just did some deeper debugging. It seems like the translation file is loaded, but when executing the function tr() this line returns false


    line 1967 in file qcoreapplication.cpp, function

    QString QCoreApplication::translate(const char *context, const char *sourceText,
                                        const char *disambiguation, Encoding encoding, int n)

    does someone know what I might be doing wrong?
    I opened the .ts file the lupdate produces, I inserted the translations, clicked on the mark as finished icon (the checksign for this text turned yellow), and I ran lrelease to get the .qm files (which are loaded successfully according to the return variable success). I just don't know what is going wrong

  • Ok found the mistake. Really stupidly, I called in main.cpp too early...

  • Lifetime Qt Champion

    So showing it before loading the translator ?

    One thing you can to is implement dynamic translation especially since you are providing several translations.

Log in to reply