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. Embed qt translation into executable in static app
Forum Updated to NodeBB v4.3 + New Features

Embed qt translation into executable in static app

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 534 Views 2 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.
  • C Offline
    C Offline
    Cobra91151
    wrote on 20 May 2023, 11:18 last edited by Cobra91151
    #1

    Hello!

    I want to embed the Qt translation files like: qt_en.qm, qt_uk.qm into my executable using resource. I have added it to the resource directory named Localization.

    bool isLoaded = qtTranslator.load(QString(":Localization/qt_uk.qm"));
    qDebug() << "isLoaded: " << isLoaded;
    

    It returns false. When I add my translation file, for example: test_uk.qm then it loads successfully. So, how to embed the Qt translations files into the executable (static program)?

    I think, I have found the issue, qt_uk.qm file size is only 1 KB, but when I check out for the same file for other program (shared), the file size is 155 KB. So, it means that Qt static also embeds the Qt localization files automatically? Then how to load them using QTranslator?

    For example, I translated my program to ukrainian language but some widgets like QMessageBox buttons and QTreeView header items are still not translated:

    localization_issue.jpg

    This translation comes from Qt files not mine. I can call setButtonText to translate the QMessageBox buttons, but how to translate the QTreeView header items like (Name, Size, Type, Date modified) which added automatically by QFileSystemModel? There should be some way to call the default Qt translation for this?
    Thanks.

    C 1 Reply Last reply 20 May 2023, 14:32
    0
    • C Cobra91151
      20 May 2023, 16:00

      @mrdebug

      Hello!

      This only works when compiling the program with Qt shared libs. Using static build I do not have any default Qt translation (qt_*) files. The Qt installation dir contains the translations files but all of them are 1 KB.

      C Offline
      C Offline
      Cobra91151
      wrote on 20 May 2023, 17:00 last edited by
      #5

      Ok. I have fixed the issue by inheriting the QFileSystemModel and overriding the headerData method. Now, it works as expected:

      2023-05-20_195952.png

      The issue is resolved. Thank you.

      C 1 Reply Last reply 24 May 2023, 16:18
      0
      • C Cobra91151
        20 May 2023, 11:18

        Hello!

        I want to embed the Qt translation files like: qt_en.qm, qt_uk.qm into my executable using resource. I have added it to the resource directory named Localization.

        bool isLoaded = qtTranslator.load(QString(":Localization/qt_uk.qm"));
        qDebug() << "isLoaded: " << isLoaded;
        

        It returns false. When I add my translation file, for example: test_uk.qm then it loads successfully. So, how to embed the Qt translations files into the executable (static program)?

        I think, I have found the issue, qt_uk.qm file size is only 1 KB, but when I check out for the same file for other program (shared), the file size is 155 KB. So, it means that Qt static also embeds the Qt localization files automatically? Then how to load them using QTranslator?

        For example, I translated my program to ukrainian language but some widgets like QMessageBox buttons and QTreeView header items are still not translated:

        localization_issue.jpg

        This translation comes from Qt files not mine. I can call setButtonText to translate the QMessageBox buttons, but how to translate the QTreeView header items like (Name, Size, Type, Date modified) which added automatically by QFileSystemModel? There should be some way to call the default Qt translation for this?
        Thanks.

        C Offline
        C Offline
        Cobra91151
        wrote on 20 May 2023, 14:32 last edited by Cobra91151
        #2

        I think, the only way is to subclass QFileSystemModel and override the headerData method.

        QVariant QFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const
        {
            switch (role) {
            case Qt::DecorationRole:
                if (section == 0) {
                    // ### TODO oh man this is ugly and doesn't even work all the way!
                    // it is still 2 pixels off
                    QImage pixmap(16, 1, QImage::Format_Mono);
                    pixmap.fill(0);
                    pixmap.setAlphaChannel(pixmap.createAlphaMask());
                    return pixmap;
                }
                break;
            case Qt::TextAlignmentRole:
                return Qt::AlignLeft;
            }
        
            if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
                return QAbstractItemModel::headerData(section, orientation, role);
        
            QString returnValue;
            switch (section) {
            case 0: returnValue = tr("Name");
                    break;
            case 1: returnValue = tr("Size");
                    break;
            case 2: returnValue =
        #ifdef Q_OS_MAC
                           tr("Kind", "Match OS X Finder");
        #else
                           tr("Type", "All other platforms");
        #endif
                   break;
            // Windows   - Type
            // OS X      - Kind
            // Konqueror - File Type
            // Nautilus  - Type
            case 3: returnValue = tr("Date Modified");
                    break;
            default: return QVariant();
            }
            return returnValue;
        }
        

        But it has tr() method on these items, so it should be already translated.
        When program compiled using Qt shared libs, I can call this:

        QString appPath = QApplication::applicationDirPath();
        qtTranslator->load(QString("%1/translations/qt_uk.qm").arg(appPath));
        

        to load the default Qt translations and it works well. The question is how to call/trigger it from the app which compiled statically?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrdebug
          wrote on 20 May 2023, 15:42 last edited by
          #3

          Hi, I think the problem is only to use the right path in the .pro and Resources.qrc file. Please have a look:

          .pro file

          RESOURCES += \
              Resources.qrc
          

          main.cpp

              QTranslator translator, translator2;
              if (translator.load("QtComPort_"+ QLocale::system().name(), ":/Translations")) a.installTranslator(&translator);
              if (translator2.load("qt_"+ QLocale::system().name(), ":/Translations")) a.installTranslator(&translator2);
          

          Resource.qrc

          <RCC>
              <qresource prefix="/Translations">
                  <file>QtComPort_it.qm</file>
                  <file>qt_it.qm</file>
              </qresource>
          </RCC>
          

          github
          https://github.com/denisgottardello/QtComPort

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          C 1 Reply Last reply 20 May 2023, 16:00
          1
          • M mrdebug
            20 May 2023, 15:42

            Hi, I think the problem is only to use the right path in the .pro and Resources.qrc file. Please have a look:

            .pro file

            RESOURCES += \
                Resources.qrc
            

            main.cpp

                QTranslator translator, translator2;
                if (translator.load("QtComPort_"+ QLocale::system().name(), ":/Translations")) a.installTranslator(&translator);
                if (translator2.load("qt_"+ QLocale::system().name(), ":/Translations")) a.installTranslator(&translator2);
            

            Resource.qrc

            <RCC>
                <qresource prefix="/Translations">
                    <file>QtComPort_it.qm</file>
                    <file>qt_it.qm</file>
                </qresource>
            </RCC>
            

            github
            https://github.com/denisgottardello/QtComPort

            C Offline
            C Offline
            Cobra91151
            wrote on 20 May 2023, 16:00 last edited by
            #4

            @mrdebug

            Hello!

            This only works when compiling the program with Qt shared libs. Using static build I do not have any default Qt translation (qt_*) files. The Qt installation dir contains the translations files but all of them are 1 KB.

            C 1 Reply Last reply 20 May 2023, 17:00
            0
            • C Cobra91151
              20 May 2023, 16:00

              @mrdebug

              Hello!

              This only works when compiling the program with Qt shared libs. Using static build I do not have any default Qt translation (qt_*) files. The Qt installation dir contains the translations files but all of them are 1 KB.

              C Offline
              C Offline
              Cobra91151
              wrote on 20 May 2023, 17:00 last edited by
              #5

              Ok. I have fixed the issue by inheriting the QFileSystemModel and overriding the headerData method. Now, it works as expected:

              2023-05-20_195952.png

              The issue is resolved. Thank you.

              C 1 Reply Last reply 24 May 2023, 16:18
              0
              • C Cobra91151 has marked this topic as solved on 20 May 2023, 17:00
              • C Cobra91151
                20 May 2023, 17:00

                Ok. I have fixed the issue by inheriting the QFileSystemModel and overriding the headerData method. Now, it works as expected:

                2023-05-20_195952.png

                The issue is resolved. Thank you.

                C Offline
                C Offline
                Cobra91151
                wrote on 24 May 2023, 16:18 last edited by Cobra91151
                #6

                Hello!

                I have found out that even when I inherit QFileSystemModel and override the headerData method, some of the data are still not translated. So, my new solution is the following.

                1. Compile the program as shared build.
                2. Get the required default Qt translation (qt_*) files.
                3. Store them to the resource file
                4. Load them together with my translations.

                Now, it translates all data. This issue is solved.

                S 1 Reply Last reply 25 May 2023, 07:10
                2
                • C Cobra91151
                  24 May 2023, 16:18

                  Hello!

                  I have found out that even when I inherit QFileSystemModel and override the headerData method, some of the data are still not translated. So, my new solution is the following.

                  1. Compile the program as shared build.
                  2. Get the required default Qt translation (qt_*) files.
                  3. Store them to the resource file
                  4. Load them together with my translations.

                  Now, it translates all data. This issue is solved.

                  S Offline
                  S Offline
                  SimonSchroeder
                  wrote on 25 May 2023, 07:10 last edited by
                  #7

                  @Cobra91151 said in Embed qt translation into executable in static app:

                  Get the required default Qt translation (qt_*) files.

                  This seems like the proper solution.

                  1 Reply Last reply
                  0

                  1/7

                  20 May 2023, 11:18

                  • Login

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