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

在cmakelist中添加下列代码

set(TS_DIR "${CMAKE_CURRENT_SOURCE_DIR}")

set(TS_FILES
    "${TS_DIR}/${PROJECT_NAME}_chinese.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})

endforeach()

会生成.ts文件和qm文件,将ts文件的翻译工作做完,再次cmake。生成的.qm文件就可以在项目中使用

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

你可能感兴趣的:(qt,c++)