Internationalization of Qt and CMake: how to update * .ts and not lose them

I have this CMakeLists.txt in the directory with the translation files ( *.ts ):

 SET(TRANSLATIONS lang_de.ts lang_en.ts ) FIND_PACKAGE(Qt5LinguistTools) QT5_ADD_TRANSLATION(QM_FILES ${TRANSLATIONS}) SET(QM_FILES ${QM_FILES} PARENT_SCOPE) ADD_CUSTOM_TARGET (translations ALL DEPENDS ${QM_FILES}) 

It builds *.qm files from the specified *.ts .

But I want to improve this and get two custom goals that won't be built automatically. One for adding new lines from sources to ts files and one for updating ts . The latter would ts from sources and remove obsolete lines from ts .

I tried adding this after the lines above:

 ADD_CUSTOM_TARGET ( ts_append COMMAND QT5_CREATE_TRANSLATION(QM_FILES ${CMAKE_SOURCE_DIR}/src/app ${TRANSLATIONS} OPTIONS -I ${CMAKE_SOURCE_DIR}/src) ) ADD_CUSTOM_TARGET ( ts_refresh COMMAND QT5_CREATE_TRANSLATION(QM_FILES ${CMAKE_SOURCE_DIR}/src/app ${TRANSLATIONS} OPTIONS -no-obsolete -I ${CMAKE_SOURCE_DIR}/src) ) 

but it seems like I can't use the QT5_CREATE_TRANSLATION macro inside a custom target, right?

Perhaps I'm wrong, how would you solve this problem: is it easy to update ts and not lose them after make clean ?

+6
source share
1 answer

To solve make clean , add a subdirectory ( ADD_SUBDIRECTORY(translations) ) and add SET_DIRECTORY_PROPERTIES(PROPERTIES CLEAN_NO_CUSTOM 1) to the contained CMakeLists.txt. See here for an example.

For the second part of your question, there are two possible ways to do this. Use either FILE(WRITE <filename> "QT5_CREATE_TRANSLATION(QM_FILES ${SOURCE_DIR}/src/app ${TRANSLATIONS} OPTIONS -I ${SOURCE_DIR}/src)") , and then use COMMAND ${CMAKE_COMMAND} -DSOURCE_DIR=${CMAKE_SOURCE_DIR} -DTRANSLATIONS=${TRANSLATIONS} <filename> in add_custom_target. I doubt there is a good way to get the contents of QM_FILES. The second option is to create two additional subdirectories, each of which has a QT5_CREATE_TRANSLATIONS and an ADD_CUSTOM_TARGET call.

+1
source

Source: https://habr.com/ru/post/970499/


All Articles