CMake如何使用lupdate和lrelease生成多国语言包qm

最近的一个项目,需要支持多国语言,即中、英、俄,以下是两种实现方式:

为了脚本简约,提前定义两个变量,分别用来存放qm和ts文件

set(TRANSLATIONS_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}/translations)
set(LANGUAGES_DIRECTORY ${PROJECT_SOURCE_DIR}/languages)

方法一:

#多语言批处理
execute_process(
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMAND "${LUPDATE_EXECUTABLE}" -recursive ./ -ts
"${LANGUAGES_DIRECTORY}/XXX_zh_CN.ts"
"${LANGUAGES_DIRECTORY}/XXX_en.ts"
"${LANGUAGES_DIRECTORY}/XXX_ru.ts"

COMMAND "${LRELEASE_EXECUTABLE}"
"${LANGUAGES_DIRECTORY}/XXX_zh_CN.ts" "${TRANSLATIONS_DIRECTORY}/XXX_zh_CN.qm"
"${LANGUAGES_DIRECTORY}/XXX_en.ts" "${TRANSLATIONS_DIRECTORY}/XXX_en.qm"
"${LANGUAGES_DIRECTORY}/XXX_ru.ts" "${TRANSLATIONS_DIRECTORY}/XXX_ru.qm"
)

方法二:

#多语言批处理

set (Langs zh_CN en_US ru_RU)
set (LUPDATE "${CMAKE_PREFIX_PATH}/bin/lupdate.exe")
set (LRELEASE "${CMAKE_PREFIX_PATH}/bin/lrelease.exe")
foreach(l ${Langs})
set (lname "${PROJECT_NAME}_${l}")
execute_process(COMMAND "${LUPDATE}" ./ -ts "${LANGUAGES_DIRECTORY}/${lname}.ts"
COMMAND "${LRELEASE}" "${LANGUAGES_DIRECTORY}/${lname}.ts" -qm "${TRANSLATIONS_DIRECTORY}/${lname}.qm"
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
endforeach() 

方法二只需要维护Langs数组即可,因此推荐大家使用!

 

posted @ 2024-04-09 16:24  快雪  阅读(126)  评论(0编辑  收藏  举报