A proper workflow for CMake + Qt translation syste

2019-04-27 08:30发布

问题:

I recently moved my Qt project form qmake to CMake build system and now I'm trying to set up a translation system. I tried to use qt5_create_translation macro (documented here) to update my *.ts files and generate *.qm files. By default the macro creates (or updates) *.ts files in the source directory and *.qm files in the build directory, which is pretty logical and convenient to use. The nature of underlying lupdate utility is such that *.ts files cannot be destroyed - they may be updated only. So I see my workflow as follows:

  1. Add or modify source or UI files.
  2. Rebuild the project: new translatable strings will be added to *.ts files.
  3. Add actual translations to *.ts now or commit changes 'as is' in order to add the translations later.
  4. As soon as translations are added to *.ts, rebuild the project to obtain the up-to-date *.qm files.

With this workflow *.ts files are almost always synchronized with source files and *.qm files are generated every time you build the project. But the ideology of CMake and/or Qt5 plugin for CMake follows another way. CMake rightly accounts *.ts files as build artifacts, so it generates a rule to delete them (from source tree (!)) on make clean. This behavior was at least twice registered as a bug, but maintainers seem to insist it's not a bug, but a feature.

I found an advice to add a subdirectory with translations and CLEAN_NO_CUSTOM on it, but these modifications break the build: adding *.qm file to executable target does not add the subdirectory to executable dependency list. So build build fails to find *.qm files and stops.

Adding custom CMake target and\or command invoking lupdate derogates all advantages of Qt5LinguistTools module and qt5_create_translation macro.

Adding a CMake option like here looks like an ugly workaround and does not keep the developer from loosing uncommitted translation by accidental invocation of make clean when UPDATE_TRANSLATIONS is turned on.

So what is the proper way to use qt5_create_translation macro with it's present-day behavior?

回答1:

without do ADD_SUBDIRECTORY,

I just add SET_DIRECTORY_PROPERTIES(PROPERTIES CLEAN_NO_CUSTOM TRUE)

before calling

QT5_CREATE_TRANSLATION