Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt internationalization and CMake: how to update *.ts and don't lose them

Tags:

cmake

qt

qt5

I'm having this CMakeLists.txt in directory with 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 specified *.ts.

But I want to improve this and get two custom targets, which won't built automatically. One for appending new strings from sources into ts files, and one for refreshing ts. The last one would update ts from sources and remove obsolete strings from ts.

I've tried to add this after 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 I can't use QT5_CREATE_TRANSLATION macro inside custom target, isn't it?

Maybe I'm on wrong way, how would you solve this problem: easy updating of ts and don't lose them after make clean?

like image 600
Storm Avatar asked Jun 07 '14 09:06

Storm


1 Answers

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

For the second part of your question there are two possible ways to do it. Either use 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's a good way of retrieving the contents of QM_FILES though. The second option is creating two additional sub directories, each with a QT5_CREATE_TRANSLATIONS and a ADD_CUSTOM_TARGET call.

like image 96
Jan Avatar answered Nov 07 '22 10:11

Jan