自学内容网 自学内容网

c++ QT中cmake项目,直接在cmakelist中添加翻译设置

在cmakelist中添加下列代码

方法一、

set(TS_FILES
   ${CMAKE_CURRENT_SOURCE_DIR}/OCCQt_zh_CN.ts
)

find_program(LUPDATE_EXECUTABLE lupdate)
find_program(LRELEASE_EXECUTABLE lrelease)

foreach(_chinese_file ${TS_FILES})

    execute_process(
        COMMAND ${LUPDATE_EXECUTABLE} -recursive ${CMAKE_SOURCE_DIR} -ts ${_chinese_file})
    execute_process(
        COMMAND ${LRELEASE_EXECUTABLE} ${_chinese_file})
    execute_process(COMMAND ${CMAKE_COMMAND} -E copy 
${CMAKE_CURRENT_SOURCE_DIR}/OCCQt_zh_CN.qm translations/)

endforeach()

第一个execute_process是生成.ts,第二个execute_process是生成.qm,第三个execute_process是复制.qm到执行目录。

进行cmake,生成的.qm文件就可以在项目中使用:

    QTranslator translator;
    translator.load(":/translations/OCCQt_zh_CN.qm", app.applicationDirPath());
    app.installTranslator(&translator)

方法二、

set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}")

set(languages
    chinese
    traditional
)
set(files ${files} resources/resources.qrc)
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${files})
find_program(LUPDATE_EXECUTABLE lupdate)
find_program(LRELEASE_EXECUTABLE lrelease)
foreach(_lan ${languages})
    set(_ts_file "${TS_DIR}/${PROJECT_NAME}_${_lan}.ts")
     execute_process(
       COMMAND ${LUPDATE_EXECUTABLE} -recursive ${CMAKE_SOURCE_DIR}/srcs -ts ${_ts_file})
           execute_process(
        COMMAND ${LRELEASE_EXECUTABLE} ${_ts_file}
    )
        execute_process(
        COMMAND ${CMAKE_COMMAND} -E copy ${TS_DIR}/${PROJECT_NAME}_${_lan}.qm translations/
    )
endforeach()


原文地址:https://blog.csdn.net/weixin_44733606/article/details/144305903

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!