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. Correct way to load .qm files in QTranslator
Qt 6.11 is out! See what's new in the release blog

Correct way to load .qm files in QTranslator

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 3 Posters 3.8k 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.
  • T Offline
    T Offline
    therealmatiss
    wrote on last edited by therealmatiss
    #1

    Hello!

    I am adding translation layer to my Qt6/QML application and I'm having issues loading my .em file.

    I am following the QML documentation, which is lacking for Localization, to say the least. The closest thing I found for me as I only need to use .qml file localization is (https://doc.qt.io/qt-6/qtlinguist-arrowpad-example.html) and (https://doc.qt.io/qt-6/qtqml-qml-i18n-example.html)

    Things I've done:

    1. Include LinguistsTools in CMake (Works)
    2. Added this to my CMakeLists.txt (It builds the .em file in my build folder)
    qt6_add_translations(${CMAKE_PROJECT_NAME}
      TS_FILES qml/translations/EN_en.ts
    )
    
    1. Added this to my main.cpp (Doesn't work!)
      QTranslator translator;
      QString translationPath = app.applicationDirPath() + "/EN_en.qm";
      if (translator.load(translationPath)) {
        app.installTranslator(&translator);
        SPDLOG_INFO("Translator loaded");
      } else {
        SPDLOG_ERROR("Failed to load translator");
      }
    

    This command translator.load(translationPath) returns false.

    I'm pretty sure my path is wrong here - how can I add correct .em file path? I don't want to hardcode it, I want to be relative - how can I get the build folder via QApplication object? Or maybe is it better to define it in QRC file and use qrc:/translations/EN_en.qm or something like that? If so, then is it a good idea to add build folder files in QRC - isn't that against the standard? In the future I will want to add translations to other languages as well so I want to get the .em file saving right.

    This is my EN_en.ts file:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE TS>
    <TS version="2.1" language="en">
        <context>
            <name>Test</name>
            <message>
                <source>Hello World</source>
                <translation>Hello World EN</translation>
            </message>
            <message>
                <source>Goodbye</source>
                <translation>Goodbye EN</translation>
            </message>
        </context>
    </TS>
    

    In QML I am testing like this - it should be Hello World EN when I get it working.

    text: qsTr("Hello World")
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • T therealmatiss

      @Christian-Ehrlicher Yes, it's in int main(), same as the QApplication. Ok, thanks for showing that it adds it to resource file automatically. Unfortunately it still doesn't work. Now I have this setup:

      CMakeLists.txt

      qt6_add_translations(${CMAKE_PROJECT_NAME}
        TS_FILES qml/translations/EN_en.ts
        RESOURCE_PREFIX "/translations"
      )
      

      main.cpp [int main()] - translator.load("qrc:/translations/EN_en") still returns false :(

      ...
      QTranslator translator;
        if (translator.load("qrc:/translations/EN_en")) {
          app.installTranslator(&translator);
          SPDLOG_INFO("Translator loaded");
        } else {
          SPDLOG_ERROR("Failed to load translator");
        }
      ...
      
      
      kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #6

      @therealmatiss , try

      if  (translator.load(":/translations/EN_en.qm")) {
      

      Director R&D, The Qt Company

      1 Reply Last reply
      2
      • T therealmatiss

        Hello!

        I am adding translation layer to my Qt6/QML application and I'm having issues loading my .em file.

        I am following the QML documentation, which is lacking for Localization, to say the least. The closest thing I found for me as I only need to use .qml file localization is (https://doc.qt.io/qt-6/qtlinguist-arrowpad-example.html) and (https://doc.qt.io/qt-6/qtqml-qml-i18n-example.html)

        Things I've done:

        1. Include LinguistsTools in CMake (Works)
        2. Added this to my CMakeLists.txt (It builds the .em file in my build folder)
        qt6_add_translations(${CMAKE_PROJECT_NAME}
          TS_FILES qml/translations/EN_en.ts
        )
        
        1. Added this to my main.cpp (Doesn't work!)
          QTranslator translator;
          QString translationPath = app.applicationDirPath() + "/EN_en.qm";
          if (translator.load(translationPath)) {
            app.installTranslator(&translator);
            SPDLOG_INFO("Translator loaded");
          } else {
            SPDLOG_ERROR("Failed to load translator");
          }
        

        This command translator.load(translationPath) returns false.

        I'm pretty sure my path is wrong here - how can I add correct .em file path? I don't want to hardcode it, I want to be relative - how can I get the build folder via QApplication object? Or maybe is it better to define it in QRC file and use qrc:/translations/EN_en.qm or something like that? If so, then is it a good idea to add build folder files in QRC - isn't that against the standard? In the future I will want to add translations to other languages as well so I want to get the .em file saving right.

        This is my EN_en.ts file:

        <?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE TS>
        <TS version="2.1" language="en">
            <context>
                <name>Test</name>
                <message>
                    <source>Hello World</source>
                    <translation>Hello World EN</translation>
                </message>
                <message>
                    <source>Goodbye</source>
                    <translation>Goodbye EN</translation>
                </message>
            </context>
        </TS>
        

        In QML I am testing like this - it should be Hello World EN when I get it working.

        text: qsTr("Hello World")
        
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @therealmatiss said in Correct way to load .qm files in QTranslator:

        QTranslator translator

        How long does this object live?

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

        T 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @therealmatiss said in Correct way to load .qm files in QTranslator:

          QTranslator translator

          How long does this object live?

          T Offline
          T Offline
          therealmatiss
          wrote on last edited by therealmatiss
          #3

          @Christian-Ehrlicher Until the end of program. It's not a problem about QTranslator - it's a problem about loading the .em file.

          There is nothing in documentation about the pathing of .qm files - where they are generated, where they should be located preferably, how they should be preferably loaded. Most of the tutorials are based from pre-Qt6 time with .pro files. For example, this (https://doc.qt.io/qt-6/qtqml-qml-i18n-example.html) is unfinished - it doesn't even show QTranslator object creation, let alone loading the .qm files.

          I just want an example on how to add .ts files to QML in Qt6 using CMake - I am not using the device locale.

          EDIT: I figured that I add that translator.load(translationPath) returns false. Must be my translationPath issue.

          Christian EhrlicherC 1 Reply Last reply
          0
          • T therealmatiss

            @Christian-Ehrlicher Until the end of program. It's not a problem about QTranslator - it's a problem about loading the .em file.

            There is nothing in documentation about the pathing of .qm files - where they are generated, where they should be located preferably, how they should be preferably loaded. Most of the tutorials are based from pre-Qt6 time with .pro files. For example, this (https://doc.qt.io/qt-6/qtqml-qml-i18n-example.html) is unfinished - it doesn't even show QTranslator object creation, let alone loading the .qm files.

            I just want an example on how to add .ts files to QML in Qt6 using CMake - I am not using the device locale.

            EDIT: I figured that I add that translator.load(translationPath) returns false. Must be my translationPath issue.

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

            @therealmatiss said in Correct way to load .qm files in QTranslator:

            Until the end of program.

            Not if your snippet you showed us is in a function.

            The cmake macro adds the qm file to the resource file so your path is wrong: https://doc.qt.io/qt-6/qtlinguist-cmake-qt-add-translations.html#embedding-generated-qm-files-in-resources

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

            T 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @therealmatiss said in Correct way to load .qm files in QTranslator:

              Until the end of program.

              Not if your snippet you showed us is in a function.

              The cmake macro adds the qm file to the resource file so your path is wrong: https://doc.qt.io/qt-6/qtlinguist-cmake-qt-add-translations.html#embedding-generated-qm-files-in-resources

              T Offline
              T Offline
              therealmatiss
              wrote on last edited by
              #5

              @Christian-Ehrlicher Yes, it's in int main(), same as the QApplication. Ok, thanks for showing that it adds it to resource file automatically. Unfortunately it still doesn't work. Now I have this setup:

              CMakeLists.txt

              qt6_add_translations(${CMAKE_PROJECT_NAME}
                TS_FILES qml/translations/EN_en.ts
                RESOURCE_PREFIX "/translations"
              )
              

              main.cpp [int main()] - translator.load("qrc:/translations/EN_en") still returns false :(

              ...
              QTranslator translator;
                if (translator.load("qrc:/translations/EN_en")) {
                  app.installTranslator(&translator);
                  SPDLOG_INFO("Translator loaded");
                } else {
                  SPDLOG_ERROR("Failed to load translator");
                }
              ...
              
              
              kkoehneK 1 Reply Last reply
              0
              • T therealmatiss

                @Christian-Ehrlicher Yes, it's in int main(), same as the QApplication. Ok, thanks for showing that it adds it to resource file automatically. Unfortunately it still doesn't work. Now I have this setup:

                CMakeLists.txt

                qt6_add_translations(${CMAKE_PROJECT_NAME}
                  TS_FILES qml/translations/EN_en.ts
                  RESOURCE_PREFIX "/translations"
                )
                

                main.cpp [int main()] - translator.load("qrc:/translations/EN_en") still returns false :(

                ...
                QTranslator translator;
                  if (translator.load("qrc:/translations/EN_en")) {
                    app.installTranslator(&translator);
                    SPDLOG_INFO("Translator loaded");
                  } else {
                    SPDLOG_ERROR("Failed to load translator");
                  }
                ...
                
                
                kkoehneK Offline
                kkoehneK Offline
                kkoehne
                Moderators
                wrote on last edited by
                #6

                @therealmatiss , try

                if  (translator.load(":/translations/EN_en.qm")) {
                

                Director R&D, The Qt Company

                1 Reply Last reply
                2
                • T Offline
                  T Offline
                  therealmatiss
                  wrote on last edited by
                  #7

                  @kkoehne Sorry for the late response but thank you! It now loads correctly.

                  Is it that QResources, which are not defined in .qrc file, are accessible by : instead of qrc:? I have only been using resources via qrc file so I haven't experienced this before. I suggest to add here (https://doc.qt.io/qt-6/qtlinguist-cmake-qt-add-translations.html#embedding-generated-qm-files-in-resources) an example on how to load correct auto-added .qm resource path, after generating .qm file.

                  There are a few more things I would like to add - please, provide a .ts file example in the documents (there's one that only explains all parameters but doesn't show an example)

                  kkoehneK 1 Reply Last reply
                  0
                  • T therealmatiss has marked this topic as solved on
                  • T therealmatiss

                    @kkoehne Sorry for the late response but thank you! It now loads correctly.

                    Is it that QResources, which are not defined in .qrc file, are accessible by : instead of qrc:? I have only been using resources via qrc file so I haven't experienced this before. I suggest to add here (https://doc.qt.io/qt-6/qtlinguist-cmake-qt-add-translations.html#embedding-generated-qm-files-in-resources) an example on how to load correct auto-added .qm resource path, after generating .qm file.

                    There are a few more things I would like to add - please, provide a .ts file example in the documents (there's one that only explains all parameters but doesn't show an example)

                    kkoehneK Offline
                    kkoehneK Offline
                    kkoehne
                    Moderators
                    wrote on last edited by
                    #8

                    @therealmatiss said in Correct way to load .qm files in QTranslator:

                    Is it that QResources, which are not defined in .qrc file, are accessible by : instead of qrc:?

                    No, it's simpler: qrc:/ is a URL, :/ is a (special) file path. API's expecting a file path (QString) only acknowledge :/, while API's expecting a URL (QUrl) only acknowledge qrc:/. Traditional API's in Qt Core typically expect QString, while QML slowly moved towards QUrl for network transparency, so it became rather confusing :/

                    Director R&D, The Qt Company

                    T 1 Reply Last reply
                    2
                    • kkoehneK kkoehne

                      @therealmatiss said in Correct way to load .qm files in QTranslator:

                      Is it that QResources, which are not defined in .qrc file, are accessible by : instead of qrc:?

                      No, it's simpler: qrc:/ is a URL, :/ is a (special) file path. API's expecting a file path (QString) only acknowledge :/, while API's expecting a URL (QUrl) only acknowledge qrc:/. Traditional API's in Qt Core typically expect QString, while QML slowly moved towards QUrl for network transparency, so it became rather confusing :/

                      T Offline
                      T Offline
                      therealmatiss
                      wrote on last edited by
                      #9

                      @kkoehne Thank you for the explanation!

                      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