PDA

View Full Version : Qt Linguist How to do translation with a CMake project?



pixaeiro
13th January 2016, 14:35
Hello,

I am developing an application with Qt 5.3, generating the project files with CMake and building with Visual Studio 2013.

Now I need to start working on the translation of the software, but I am kind of lost on how to do this with a CMake project.

I found some info in the Qt CMake manual on the Qt5LinguistTools, I added the macros to my CMake, but I don't know when are the qm and ts files generated, or how I am supposed to add them to the project.

I added the FIND_PACKAGE macro:
SET ( Qt5LinguistTools_DIR "C:/Qt/5.3.2/vs2013_64bits/qtbase/lib/cmake/Qt5LinguistTools" CACHE PATH "Qt5LinguistTools_DIR" )
FIND_PACKAGE( Qt5LinguistTools )

I added the QT5_CREATE_TRANSLATION macro:
QT5_CREATE_TRANSLATION( QT_TRANSLATIONS_FILES ${MOC_HEADER_FILES} ${QT_SOURCE_FILES} )

And finally I added the QT5_ADD_TRANSLATION macro:
QT5_ADD_TRANSLATION( QT_TRANSLATIONS_FILES )

But after configuring and generating the CMake build project, and opening and building in Visual Studio, I don't see anything new or different.

Am I doing this right? Am I supposed to add some files as sources in the ADD_EXECUTABLE macro?

Any help will be greatly appreciated! Thanks!

PierreA
17th July 2017, 09:31
I've been looking for the way to do this in CMake. I find pages that were written years ago saying that the capability to add custom build rules is intentionally disabled in CMake, for reasons unexplained, with no explanation how to enable it. It looks like the way to do it is:
1. List all .ts files in the source directory.
2. Run the list through sed, changing the source directory to the build directory and the extension to .qm .
3. Run the CMake add_custom_command command on each .qm file (whether it exists or not), telling it that it can be made by running lrelease on the corresponding .ts file.
As to updating the .ts files, they are source files which have to be manually edited by translators, so they can't be updated by CMake. Use a shell script that just does "lupdate *.cpp -ts *.ts".

Infinity
17th July 2017, 23:21
You found the documentation? http://doc.qt.io/qt-5/cmake-manual.html

Using the described macros makes adding further translations, updating existing translations and releasing them is quite simple.

Using those macros, my projects only require extending a variable to add further translations: https://github.com/Martchus/syncthingtray#adding-translations (CMake module were magic happens: https://github.com/Martchus/qtutilities/blob/master/cmake/modules/QtConfig.cmake#L125)

So there is no need to use a shell script for calling lupdate. My CMake module also adds install targets for the translations. It even allows to optionally include the translations as resources using the Qt resource system (so you don't have extra files flying around your statically linked binary).