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. Manage translations with CMake

Manage translations with CMake

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 2.4k Views
  • 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
    asc7uni
    wrote on last edited by
    #1

    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
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • A Offline
        A Offline
        asc7uni
        wrote on last edited by
        #3

        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
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • A Offline
            A Offline
            asc7uni
            wrote on last edited by
            #5

            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
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              2
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                2
                • A Offline
                  A Offline
                  asc7uni
                  wrote on last edited by asc7uni
                  #8

                  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
                  0

                  • Login

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