Solved Manage translations with CMake
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.
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
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.
Works fine for me:
Found 1 source text(s) (0 new and 1 already existing)
It seems I found a culprit:
cmake --build <build_path> --target cleanwhen 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?
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.
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.
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