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. Load .qm file from resource
Forum Updated to NodeBB v4.3 + New Features

Load .qm file from resource

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 7 Posters 13.2k Views 4 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.
  • beeckscheB beecksche

    Hi,
    i try to load a .qm file from my resource:

    int main(int argc, char *argv[])
    {
        Q_INIT_RESOURCE(resource);
    
        QApplication app(argc, argv);
    
        QTranslator translator;
    
        if (translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/translations")))
            app.installTranslator(&translator);
    
        MainWindow w;
        w.showMaximized();
    
        return app.exec();
    }
    

    The resource.qrc

    <RCC>
        <qresource prefix="/translations">
            <file>resources/qt_de.qm</file>
        </qresource>
    </RCC>
    

    But the load function always returns false.

    Where is my mistake?
    Thanks

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #3

    Try QLatin1String(":/translations/resources") or alias the file location in the resource file.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    1
    • beeckscheB Offline
      beeckscheB Offline
      beecksche
      wrote on last edited by
      #4

      @mrjj , @kshegunov

      So i tried it all, but the QTranslator won't load the file.

      It's strange beacuase i can load and open the file with QFile.

          QFile file;
          file.setFileName(":/translations/resources/qt_de.qm");
          qDebug() << file.open(QIODevice::ReadOnly); // true
          qDebug() << translator.load(":/translations/resources/qt_de.qm"); // false
      
      kshegunovK 1 Reply Last reply
      0
      • beeckscheB beecksche

        @mrjj , @kshegunov

        So i tried it all, but the QTranslator won't load the file.

        It's strange beacuase i can load and open the file with QFile.

            QFile file;
            file.setFileName(":/translations/resources/qt_de.qm");
            qDebug() << file.open(QIODevice::ReadOnly); // true
            qDebug() << translator.load(":/translations/resources/qt_de.qm"); // false
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #5

        @beecksche said in Load .qm file from resource:

        qDebug() << translator.load("qt_de", ":/translations/resources");
        

        ?

        Read and abide by the Qt Code of Conduct

        beeckscheB 1 Reply Last reply
        1
        • kshegunovK kshegunov

          @beecksche said in Load .qm file from resource:

          qDebug() << translator.load("qt_de", ":/translations/resources");
          

          ?

          beeckscheB Offline
          beeckscheB Offline
          beecksche
          wrote on last edited by beecksche
          #6

          @kshegunov

          My test results

          QFile file;
          file.setFileName(":/translations/resources/qt_de.qm");
          qDebug() << file.open(QIODevice::ReadOnly);
          qDebug() << translator.load(QLatin1String(":/translations/resources/qt_de"));
          qDebug() << translator.load(QLatin1String(":/translations/resources/qt_de.qm"));
          qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String(":/translations"));
          qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String(":/translations/resources"));
          qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/translations"));
          qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/translations/resources"));
          
          true
          false
          false
          false
          false
          false
          false
          
          kshegunovK 1 Reply Last reply
          0
          • beeckscheB beecksche

            @kshegunov

            My test results

            QFile file;
            file.setFileName(":/translations/resources/qt_de.qm");
            qDebug() << file.open(QIODevice::ReadOnly);
            qDebug() << translator.load(QLatin1String(":/translations/resources/qt_de"));
            qDebug() << translator.load(QLatin1String(":/translations/resources/qt_de.qm"));
            qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String(":/translations"));
            qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String(":/translations/resources"));
            qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/translations"));
            qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/translations/resources"));
            
            true
            false
            false
            false
            false
            false
            false
            
            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #7

            Okay. That's information aplenty. Could you try the same, but without putting the translation into a resource? Put them in a regular directory and supply the absolute dir path instead of :/translations/resources?

            Read and abide by the Qt Code of Conduct

            beeckscheB 1 Reply Last reply
            0
            • kshegunovK kshegunov

              Okay. That's information aplenty. Could you try the same, but without putting the translation into a resource? Put them in a regular directory and supply the absolute dir path instead of :/translations/resources?

              beeckscheB Offline
              beeckscheB Offline
              beecksche
              wrote on last edited by
              #8

              @kshegunov

              QFile file;
              file.setFileName("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de.qm");
              qDebug() << file.open(QIODevice::ReadOnly);
              qDebug() << translator.load(QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de"));
              qDebug() << translator.load(QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de.qm"));
              qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations"));
              qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations"));
              
              true
              false
              false
              true
              true
              

              If i use the file outside the resource i can load it!

              raven-worxR 1 Reply Last reply
              0
              • beeckscheB beecksche

                @kshegunov

                QFile file;
                file.setFileName("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de.qm");
                qDebug() << file.open(QIODevice::ReadOnly);
                qDebug() << translator.load(QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de"));
                qDebug() << translator.load(QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de.qm"));
                qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations"));
                qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations"));
                
                true
                false
                false
                true
                true
                

                If i use the file outside the resource i can load it!

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by raven-worx
                #9

                @beecksche
                the problem is the default compression of qrc.
                QTranslator checks for qrc files and only loads them when they are uncompressed (don't ask me whats the reason for that is).

                Try to add the following to your .pro file:

                QMAKE_RESOURCE_FLAGS += -no-compress
                

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                beeckscheB 1 Reply Last reply
                2
                • raven-worxR raven-worx

                  @beecksche
                  the problem is the default compression of qrc.
                  QTranslator checks for qrc files and only loads them when they are uncompressed (don't ask me whats the reason for that is).

                  Try to add the following to your .pro file:

                  QMAKE_RESOURCE_FLAGS += -no-compress
                  
                  beeckscheB Offline
                  beeckscheB Offline
                  beecksche
                  wrote on last edited by
                  #10

                  @raven-worx
                  Thanks for the information. I added

                  QMAKE_RESOURCE_FLAGS += -no-compress
                  

                  Run qmake and build it again, but still not able to load it from resource.

                  raven-worxR 1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #11

                    As a referense: nice article by @ekkescorner : https://appbus.wordpress.com/2016/04/28/howto-translations-i18n/

                    Try moving the .qm files in the main directory rather than the resources one and see if that works

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    2
                    • beeckscheB beecksche

                      @raven-worx
                      Thanks for the information. I added

                      QMAKE_RESOURCE_FLAGS += -no-compress
                      

                      Run qmake and build it again, but still not able to load it from resource.

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by raven-worx
                      #12

                      @beecksche
                      can you try this workaround:

                      1. create a .rcc file with rcc -no-compress mytranslations.qrc -o mytranslations.rcc
                      2. add this .rcc file to your qrc file
                      3. load the .rcc file with
                      QFile translations(":/mytranslations.rcc");
                      translations.open(QIODevice::ReadOnly);
                      QResource::registerResource( (const uchar *)translations.readAll().constData() );
                      translator.load( ... );
                      

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      1
                      • J Offline
                        J Offline
                        jmg227
                        wrote on last edited by
                        #13

                        I am having the same problem. I found the following page
                        http://doc.qt.io/qt-5/linguist-programmers.html which describes using lconvert to create a qt_de.qm file with only the required modules, e.g.:

                        lconvert -o installation_folder/qt_de.qm qtbase_de.qm qtdeclarative_de.qm
                        

                        I found that if I created the .qm file in my resource file this way, I could then load the qt translation from my resource file.

                        It is interesting that the size of qt_de.qm in C:/Qt/Qt5.7.0/5.7/mingw53_32/translations is 1 KB. The qt_de.qm that is generated by lconvert is 239 KB.

                        1 Reply Last reply
                        1
                        • Dmitiy TyuginD Offline
                          Dmitiy TyuginD Offline
                          Dmitiy Tyugin
                          wrote on last edited by
                          #14

                          Probably the problem in prefix. Here are my files from working project

                          resources.qrc

                          <RCC>
                              <qresource prefix="/">
                                  <file>assets/translations/language_ru.qm</file>
                                  <file>assets/translations/language_en.qm</file>
                              </qresource>
                          </RCC>
                          

                          Folders structure:

                          ProjectRoot/project.pro
                          ProjectRoot/resources.qrc
                          ProjectRoot/assets/translations/language_ru.qm
                          ProjectRoot/assets/translations/language_ru.qm

                          Code that works ok

                          QTranslator tr;
                          tr.load(":/assets/translations/language_ru.qm")
                          
                          1 Reply Last reply
                          1
                          • J Offline
                            J Offline
                            jmg227
                            wrote on last edited by
                            #15

                            I would like to clarify something. I do not think that the problem the original poster has is in the prefix. As I said, I believe I was having the same problem. My application's translation file, dwb_lang_de.qm, can be loaded by QTranslator without any problem. My problem came about when I tried to load Qt's translation file, qt_de.qm. If you look at the Qt translation files, (I'm running on windows so they are in C:\Qt\Qt5.7.0\5.7\mingw53_32\translations), you will see that qt_de.qm is suspiciously small. In fact, the Qt Linguist Manual (URL is above) states:
                            In Qt 5, the .qm files were split up by module and there is a so-called meta catalog file which includes the .qm files of all modules. The name of the meta catalog file is identical to the name of Qt 4's monolithic .qm file so that existing loader code works as before provided all included .qm files are found.

                            A solution is to use lconvert, as I suggested above, to build your own qt_de.qm that contains the strings for the modules that you need. This can then be added to your resource file and QTranslator can load it without any problem.

                            Since posting that, I have found that an alternate solution is to put all of the .qm files for Qt (assistant_cs.qm, assistant_da.qm, assistant_de.qm, ...) into your resource file. Then when you load the meta-file, qt_de.qm, it will load fine because it will find all the files it references.

                            Either method works fine.

                            1 Reply Last reply
                            2

                            • Login

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