Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot load .qm translation file
Qt 6.11 is out! See what's new in the release blog

Cannot load .qm translation file

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.5k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    andrezzvazz
    wrote on last edited by
    #1

    Hello,

    I'm new to QT so, if I do something wrong, please forgive me :p

    I'm trying to do a small application to test the translation tools of QT.
    I've generated the .qm file from the Linguist. I've put it in the resources. I can test that the file is there with the QFile::exists(":/portuguese.qm") which returns true

    • My main.cpp:
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTranslator t;
        QStringList languages;
        languages << "English"<<"Portuguese";
    
        QString lang = QInputDialog::getItem(NULL,"Select Language","Language", languages);
    
    
        if(!QFile::exists(":/portuguese.qm"))
            qWarning("failed-no file");
        else
            qWarning("File Found");
    
        if(lang == "Portuguese")
        {
            if ( t.load(":/portuguese.qm") )
            {
                a.installTranslator(&t);
                qDebug() << "File loaded";
            } else {
                qWarning() << "File not loaded";
            }
        }
    
        if (lang != "English")
        {
            a.installTranslator(&t);
        }
    
    
    
        Widget w;
        w.show();
        return a.exec();
    }
    
    • My translationfile.qrc
    <RCC>
        <qresource prefix="/">
            <file>portuguese.qm</file>
        </qresource>
    </RCC>
    
    • My TranslationExample.pro
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++11
    
    # You can make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
        main.cpp \
        widget.cpp
    
    HEADERS += \
        widget.h
    
    FORMS += \
        widget.ui
    
    TRANSLATIONS += \
        portuguese.ts
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    DISTFILES += \
        portuguese.qm
    
    RESOURCES += \
        translationfile.qrc
    

    I've tried to load the .qm file in a lot of different ways, but non of them work. Any kind of help will be preciouses.

    Best regards,
    André Vaz

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You're using QTranslator::load() wrong.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andrezzvazz
        wrote on last edited by
        #3

        I've saw in a quick YouTube tutorial, some guy just use it in that way. Any way, I've try to use the load method in a lot different ways and none of them work.

        Christian EhrlicherC 1 Reply Last reply
        0
        • A andrezzvazz

          I've saw in a quick YouTube tutorial, some guy just use it in that way. Any way, I've try to use the load method in a lot different ways and none of them work.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @andrezzvazz said in Cannot load .qm translation file:

          Any way, I've try to use the load method in a lot different ways and none of them work.

          Please show some code how you tried.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andrezzvazz
            wrote on last edited by
            #5

            There you have it!

            if(lang == "Portuguese")
                {
                    qDebug() << t.load(":/portuguese.qm");
                    qDebug() << t.load(":/portuguese");
                    qDebug() << t.load("portuguese");
                    qDebug() << t.load(QLocale(), QLatin1String("portuguese"), QLatin1String(":/"));
                    qDebug() << t.load(QLocale(), QLatin1String("portuguese.qm"), QLatin1String(":/"));
                }
            

            Each one of these throw false...

            Pablo J. RoginaP 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              They all can't work because you don't pass the correct directory (second parameter for the first three, forth parameter for the other two). prefix != directory!

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • A andrezzvazz

                There you have it!

                if(lang == "Portuguese")
                    {
                        qDebug() << t.load(":/portuguese.qm");
                        qDebug() << t.load(":/portuguese");
                        qDebug() << t.load("portuguese");
                        qDebug() << t.load(QLocale(), QLatin1String("portuguese"), QLatin1String(":/"));
                        qDebug() << t.load(QLocale(), QLatin1String("portuguese.qm"), QLatin1String(":/"));
                    }
                

                Each one of these throw false...

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #7

                @andrezzvazz you may want to check this answer in Stack Overflow.

                Interesting is the part where you can set the language for every file into the resource file itself and then Qt load the proper file based on the locale in use.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                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