Qt internationalization and CMake: how to update *

2019-04-22 20:51发布

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?

标签: qt cmake qt5
1条回答
\"骚年 ilove
2楼-- · 2019-04-22 20:56

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.

查看更多
登录 后发表回答