Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Manage translations with CMake

    General and Desktop
    2
    8
    963
    Loading More Posts
    • 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
      asc7uni last edited by

      Hello everyone!

      I am trying to migrate from .pro files to CMake and stuck on automation of preparing and embedding translations into executable.

      Is there working example of CMake project that will automatically build and embed translation resources to application? One I could download and it will work out of the box so I can use it as reference.

      I tried to accomplish something basing on few examples in the network without success. I followed this article https://doc.qt.io/qt-5/qtlinguist-cmake-qt5-create-translation.html and also this one https://developpaper.com/configure-clion-to-manage-qt-project-internationalization-support/

      Probably I am missing something obvious.

      Just in case I prepared minimal not working example https://github.com/pozemka/not-working-translation if you want to point to my mistakes or try and build it by yourself.

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        You should add QM_FILES to your add_executable() call - otherwise there is no dependency that they're created. And then you must fix the path in your qrc file - the qm files are created directly in the build dir, not in lang

        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 Reply Quote 1
        • A
          asc7uni last edited by

          Thank you very much, Christian! This helps. Partially :)

          Now .qm files are successfully generated and loaded from resources on application start.

          The issue is that every time I rebuild project, .ts files are regenerated as new, removing all previous translated content:

          ---<translation>Привет!</translation>
          +++<translation type="unfinished"></translation>
          

          This is what I get from QtCreator's build output:

          [ 10%] Automatic MOC and UIC for target translation
          [ 10%] Built target translation_autogen
          [ 20%] Generating /home/uni/projects/tryouts/translation-cmake/lang/ru.ts
          Scanning directory '/home/uni/projects/tryouts/translation-cmake'...
          Updating '../translation-cmake/lang/ru.ts'...
              Found 1 source text(s) (1 new and 0 already existing)
          #I cut fr.ts part. It is same as ru.ts
          [ 50%] Generating ru.qm
          Updating '/home/uni/projects/tryouts/build-translation-cmake-Desktop-Debug/ru.qm'...
              Generated 0 translation(s) (0 finished and 0 unfinished)
              Ignored 1 untranslated source text(s)
          [ 60%] Automatic RCC for translations.qrc
          

          Git repo with example is updated.

          1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

            Works fine for me:

            Updating '../lang/ru.ts'...
            Found 1 source text(s) (0 new and 1 already existing)

            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 Reply Quote 0
            • A
              asc7uni last edited by

              It seems I found a culprit:

              QtCreator calls cmake --build <build_path> --target clean when rebuild is requested.
              When I ran this command in console it removed .ts files from lang/ directory.

              Probably you or someone else here know a way to keep .ts files untouched during clean process?

              1 Reply Last reply Reply Quote 0
              • Christian Ehrlicher
                Christian Ehrlicher Lifetime Qt Champion last edited by

                this is because of add_custom_command() for ludate which defines the ts file as output. Therefore cmake marks the ts file as 'GENERATED' which is wrong. Looks like a cmake macro issue.
                Switch to qt5_add_translation() and update the translations with a custom target which you have to call separately.

                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 Reply Quote 2
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion last edited by

                  Created a bug report for this: https://bugreports.qt.io/browse/QTBUG-96549 but I think this will not work out without a change in cmake.

                  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 Reply Quote 2
                  • A
                    asc7uni last edited by asc7uni

                    Thank you for your help.

                    Calling lupdate form time to time is OK.

                    I've updated repository with working code and more suitable name: https://github.com/pozemka/cmake-qt-translation-example

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post