Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved]I try to load translation file (.qm file), but nothing happened
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    owenzhao
    wrote on last edited by
    #1

    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() + "/");
    //viewer.setMainQmlFile&#40;QLatin1String("qrc:/main.qml"&#41;&#41;;
    viewer.setMainQmlFile("qrc:/main.qml"&#41;;
    
    QTranslator myTr;
    //myTr.load("qrc:/transtation_" + QLocale::system(&#41;.name(&#41;&#41;;
    myTr.load("qrc:/translation_" + QLocale::system(&#41;.name(&#41;&#41;;
    app->installTranslator(&myTr&#41;;
    
    viewer.showExpanded();
    
    return app->exec(&#41;;
    

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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        owenzhao
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

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

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            owenzhao
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SherifOmran
              wrote on last edited by
              #6

              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 ..

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved