概述
- 实验通过版本, Qt5, cmake-3.23
- 参考
- cmake中不会涉及到linguist的操作
- 当生成ts文件后, 手动使用linguist工具翻译好即可
核心代码
# i18N
function(CREATE_QM_UPDATE_TS)
set(option)
# TARGET_NAME-项目名称
set(oneValueArgs TS_ROOT TARGET_NAME)
# ALL_TS_FILE-所有文件
# ALL_TRANSLATE_ROOT-要翻译哪些文件夹下的源码文件
set(multiValueArgs ALL_TS_FILE ALL_TRANSLATE_ROOT)
# 固定写法
cmake_parse_arguments(TS_QM "${option}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
set(update_all_ts_files ${WHICH_PROJECT_NAME}_UPDATE_ALL_TS)
set(create_all_qm_files ${WHICH_PROJECT_NAME}_CREATE_QM_FILE)
add_custom_target(${update_all_ts_files} ALL)
add_custom_target(${create_all_qm_files} ALL)
# 手动查找翻译需要的程序
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
find_file(LUPDATE_PATH lupdate ${qt_bin_dir})
find_file(LRELEASE_PATH lrelease ${qt_bin_dir})
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
find_file(LUPDATE_PATH lupdate.exe ${qt_bin_dir})
find_file(LRELEASE_PATH lrelease.exe ${qt_bin_dir})
endif()
# 把Ts文件提取出来
foreach(TS_FILE ${TS_QM_ALL_TS_FILE})
# 得到文件名
get_filename_component(LOCAL_LANGUAGE_NAME ${TS_FILE} NAME_WE)
# 指定TS项目名称
set(TS_TARGET_NAME "${WHICH_PROJECT_NAME}_UPATE_TS_${LOCAL_LANGUAGE_NAME}")
# 用于更新项目文件中的翻译
add_custom_target(${TS_TARGET_NAME}
COMMAND ${LUPDATE_PATH} ${TS_QM_ALL_TRANSLATE_ROOT} -ts ${TS_FILE}
VERBATIM
)
# 将 update_ts_file_<name>添加到update_all_ts_file项目的依赖
add_dependencies(${update_all_ts_files} ${TS_TARGET_NAME})
# 准备qm文件
set(QM_TARGET_NAME "${WHICH_PROJECT_NAME}_CREATE_QM_${LOCAL_LANGUAGE_NAME}")
set(QM_FILE "${TS_QM_TS_ROOT}/${LOCAL_LANGUAGE_NAME}.qm")
# qm文件转换
add_custom_target(${QM_TARGET_NAME}
COMMAND ${LRELEASE_PATH} ${TS_FILE} -qm ${QM_FILE}
VERBATIM
)
# 因为须先有TS文件,才能生成qm文件
add_dependencies(${QM_TARGET_NAME} ${TS_TARGET_NAME})
add_dependencies(${create_all_qm_files} ${QM_TARGET_NAME})
SET_PROPERTY(TARGET ${create_all_qm_files} PROPERTY FOLDER "LANGUAGE")
SET_PROPERTY(TARGET ${QM_TARGET_NAME} PROPERTY FOLDER "LANGUAGE")
SET_PROPERTY(TARGET ${update_all_ts_files} PROPERTY FOLDER "LANGUAGE")
SET_PROPERTY(TARGET ${TS_TARGET_NAME} PROPERTY FOLDER "LANGUAGE")
endforeach(TS_FILE ${TS_QM_ALL_TS_FILE})
IF (NOT ${WHICH_PROJECT_NAME})
add_dependencies(${WHICH_PROJECT_NAME} ${QM_TARGET_NAME})
ENDIF()
endfunction()
使用范例
# ts文件所在路径
set(ALL_TS_ROOT
${CMAKE_CURRENT_SOURCE_DIR}/qrc/i18N
)
# 翻译文件
set(proj_ts_file ${CMAKE_CURRENT_SOURCE_DIR}/qrc/i18N/zh_CN.ts)
# 指定要翻译哪些文件夹
set(project_translate_root
${proj_root}
)
# 添加翻译
CREATE_QM_UPDATE_TS(TARGET_NAME ${PROJECT_NAME}
TS_ROOT ${ALL_TS_ROOT}
ALL_TS_FILE ${proj_ts_file}
ALL_TRANSLATE_ROOT ${project_translate_root}
)
- 根据生成的项目, 就能增量更新ts文件了,下面以VS项目为例,执行后的项目如下
一个完整的cmakelists.txt
cmake_minimum_required(VERSION 3.23)
project(CMakeDemo_i18N)
set(CMAKE_PREFIX_PATH
${CMAKE_PREFIX_PATH}
"C:/major/development/tools/qt/5.14/install/5.14.2/msvc2015_64"
)
# message("${third_party_root}/commu=${third_party_root}/commu")
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# include(${linguist_cmake})
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
set(QT_VERSION 5)
set(REQUIRED_LIBS_QUALIFIED Core Gui Widgets Sql SerialPort)
# CMAKE包查找
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS_QUALIFIED} REQUIRED)
# 指定源码目录
set(proj_root
${CMAKE_CURRENT_SOURCE_DIR}/src
)
# 有这些文件需要纳入编译: 不推荐这样用, 应该一个一个文件的手动加入到这里
file(GLOB_RECURSE
proj_files
${proj_root}/*.h
${proj_root}/*.cpp
)
# 指定qrc文件
set(project_qrc_file
${CMAKE_CURRENT_SOURCE_DIR}/qrc/DemoWidget.qrc
)
# -----------------------------------------------
# 创建可执行程序项目
add_executable(${PROJECT_NAME}
${proj_files}
${widget_files}
${project_qrc_file}
)
# -----------------------------------------------
# ts文件所在路径
set(ALL_TS_ROOT
${CMAKE_CURRENT_SOURCE_DIR}/qrc/i18N
)
# 翻译文件
set(proj_ts_file ${CMAKE_CURRENT_SOURCE_DIR}/qrc/i18N/zh_CN.ts)
# 指定要翻译哪些文件夹
set(project_translate_root
${proj_root}
)
# 添加翻译
CREATE_QM_UPDATE_TS(TARGET_NAME ${PROJECT_NAME}
TS_ROOT ${ALL_TS_ROOT}
ALL_TS_FILE ${proj_ts_file}
ALL_TRANSLATE_ROOT ${project_translate_root}
)
# 指定头文件搜索路径
target_include_directories(${PROJECT_NAME}
PRIVATE ${proj_root}
PRIVATE ${widget_inc}
)
# 链接Qt库
target_link_libraries(${PROJECT_NAME}
PRIVATE Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui
)
# 如果是windows,设置管理员权限。
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/level='requireAdministrator'" )
endif()
# 关闭控制台
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
endif()