[solved]I try to load translation file (.qm file), but nothing happened

  • I have three files, main.cpp, main.qml, MainPage.qml.
    First, I use @lupdate main.qml MainPage.qml -ts translation_zh_CN.ts@
    Then I translate the ts file in Qt Linguist
    Then I use @lrelease translation_zh_CN.ts@
    Then I add translation_zh_CN.qm to my resource.qrc file
    my main.cpp is like this
    @#include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include <QDebug>

    Q_DECL_EXPORT int main(int argc, char *argv[])
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QLocale test = QLocale::system&#40;&#41;;
    qDebug(&#41; << "System locale:" << test.name(&#41;;
    QmlApplicationViewer viewer;
    viewer.rootContext(&#41;->setContextProperty("installPath", "file:///" + QCoreApplication::applicationDirPath() + "/");
    QTranslator myTr;
    //myTr.load("qrc:/transtation_" + QLocale::system(&#41;.name(&#41;&#41;;
    myTr.load("qrc:/translation_" + QLocale::system(&#41;.name(&#41;&#41;;
    return app->exec(&#41;;

    the qDebug() shows my locale is zh_CN, but the app just doesn't show my translation.

  • Moderators

    You have the line
    myTr.load("qrc:/transtation_" + QLocale::system().name());

    where "translation_" is misspelled. It's probably not seeing the proper file to load.

  • Thank you. But after I change the misspelling, the result is still the same.

  • Moderators

    Does it make a difference if you load the translator before you create your QApplicationViewer?

  • Thank you. It works when I put them before I create QApplicationViewer. Also, there is something tricky that I have to use.
    @ myTr.load(":/translation_" + QLocale::system().name());@
    instead of
    @ myTr.load("qrc:/translation_" + QLocale::system().name());@

    Is there difference between them? I used to think they are the same.

  • How did you solve it i have a similar issue, it works in debug but not in release ... I have it before the viewer as well but it does not help ..

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.