使用CMake创建Qt翻译文件

时间:2016-10-06 10:15:13

标签: c++ qt cmake qt5

当我尝试在CMake过程中添加生成翻译的过程时,我遇到了问题。

现在我有以下CMakeLists.txt:

val ClicksPerDeviceid = sqlContext.sql("SELECT DeviceId, count(*) as ClickCount from adreqdata where Network = 9 and EventType = 2 group by DeviceId ")
ClicksPerDeviceid.cache()

val alladreq = sqlContext.sql("select DeviceId,count(EventType) as AllAdreqCount from adreqdata where EventType = 1 and Network = 0 group by DeviceId ")
alladreq.cache()

似乎这段代码正确生成了.qm文件,但在被资源文件读取之前没有生成。所以我收到了一个错误:configure_file(${CMAKE_CURRENT_SOURCE_DIR}/defines.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/defines.h) file(GLOB_RECURSE UI_FILES *.ui) file(GLOB_RECURSE CODE_FILES *.cpp) qt5_wrap_ui(UI_HEADERS ${UI_FILES}) # Qt5LinguistTools find_package(Qt5LinguistTools) FILE(GLOB TS_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../resources/langs/*.ts") QT5_create_translation(QM_FILES ${CODE_FILES} ${TS_FILES}) # Resources qt5_add_resources(RESOURCE_FILES ../resources/resources.qrc) # Windows application icon if (WIN32) set(WINDOWS_RES_FILE ${CMAKE_CURRENT_BINARY_DIR}/resources.obj) if (MSVC) add_custom_command(OUTPUT ${WINDOWS_RES_FILE} COMMAND rc.exe /fo ${WINDOWS_RES_FILE} resources.rc WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cmake/win ) else() add_custom_command(OUTPUT ${WINDOWS_RES_FILE} COMMAND windres.exe resources.rc ${WINDOWS_RES_FILE} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cmake/win ) endif() endif() #Move Qm files to resources langs folder install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/../resources/langs) add_executable(${CMAKE_PROJECT_NAME} WIN32 ${QM_FILES} ${UI_HEADERS} ${CODE_FILES} ${RESOURCE_FILES} ${WINDOWS_RES_FILE} ) target_link_libraries(${CMAKE_PROJECT_NAME} Qt5::Widgets QtAwesome ) if (UNIX) install(TARGETS ${CMAKE_PROJECT_NAME} RUNTIME DESTINATION bin) elseif (WIN32) install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION .) endif()

也许我没有以正确的方式做到这一点,或者我需要从不在资源文件中的其他地方读取语言文件。

您能否就如何生成QM文件并在构建过程中添加它们提供建议。

2 个答案:

答案 0 :(得分:7)

来自documentation

  

更新翻译可以通过将qm_files添加到库/可执行文件的源列表中来完成,因此它们总是会更新,或者通过添加自定义目标来控制它们何时更新/生成。

您可以创建自定义目标并添加依赖项:

add_custom_target(translations ALL DEPENDS ${QM_FILES})

add_custom_target(resources ALL DEPENDS ${RESOURCE_FILES})

add_dependencies(resources translations)

add_executable(${CMAKE_PROJECT_NAME} WIN32
  ${UI_HEADERS}
  ${CODE_FILES}
  ${RESOURCE_FILES}
  ${WINDOWS_RES_FILE}
)
add_dependencies(${CMAKE_PROJECT_NAME} resources)

答案 1 :(得分:3)

这就是我为QOwnNotes做的事:https://github.com/pbek/QOwnNotes/blob/develop/src/CMakeLists.txt

# Translation files
SET(QON_TS_FILES
languages/QOwnNotes_en.ts
        languages/QOwnNotes_de.ts
        languages/QOwnNotes_fr.ts
        languages/QOwnNotes_pl.ts
        languages/QOwnNotes_zh.ts
        languages/QOwnNotes_ru.ts
        languages/QOwnNotes_pt.ts
        languages/QOwnNotes_es.ts
        languages/QOwnNotes_nl.ts
        languages/QOwnNotes_hu.ts
        languages/QOwnNotes_ja.ts
        languages/QOwnNotes_it.ts
        languages/QOwnNotes_ar.ts
)

qt5_add_translation(QON_QM_FILES ${QON_TS_FILES})
add_custom_target(translations DEPENDS ${QON_QM_FILES})

if(NOT QT_TRANSLATIONS_DIR)
# If this directory is missing, we are in a Qt5 environment.
# Extract the qmake executable location
get_target_property(QT5_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
# Ask Qt5 where to put the translations
execute_process( COMMAND ${QT5_QMAKE_EXECUTABLE} -query QT_INSTALL_TRANSLATIONS
OUTPUT_VARIABLE qt_translations_dir OUTPUT_STRIP_TRAILING_WHITESPACE )
# make sure we have / and not \ as qmake gives on windows
file( TO_CMAKE_PATH "${qt_translations_dir}" qt_translations_dir)
set( QT_TRANSLATIONS_DIR ${qt_translations_dir} CACHE PATH
"The location of the Qt translations" FORCE)
endif()

install(FILES ${QON_QM_FILES}
DESTINATION ${QT_TRANSLATIONS_DIR})

add_executable(QOwnNotes ${SOURCE_FILES} ${RESOURCE_ADDED} ${QON_QM_FILES})

这对你有帮助吗?