cmake之Qt国际化

2022/4/21 23:13:07

本文主要是介绍cmake之Qt国际化,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

概述

  • 实验通过版本, Qt5, cmake-3.23
  • 参考
  • cmake中不会涉及到linguist的操作
  • 当生成ts文件后, 手动使用linguist工具翻译好即可

核心代码

  • 已经做好宏。 直接copy就能用
macro(_create_qm_file_ target_name qt_bin_dir  TS_FILES bin_qrc_dir update_src_dir)
        
        set(update_all_ts_files ${target_name}_update_all_ts_files)
        set(create_all_qm_files ${target_name}_create_all_qm_files)

        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()


        foreach(TS_FILE ${TS_FILES})

                # 把zh_CN.ts中的zh_CN提取出来
                get_filename_component(LOCAL_LANGUAGE_NAME ${TS_FILE} NAME_WE)

                set(TS_TARGET_NAME "${target_name}_update_ts_file_${LOCAL_LANGUAGE_NAME}")

                add_custom_target(${TS_TARGET_NAME}
                        COMMAND ${LUPDATE_PATH} ${update_src_dir} -ts ${TS_FILE}
                        VERBATIM)

                # 将update_ts_file_<NAME>添加为update_all_ts_files的依赖,下同
                add_dependencies(${update_all_ts_files} ${TS_TARGET_NAME})

                set(QM_TARGET_NAME "${target_name}_create_qm_file_${LOCAL_LANGUAGE_NAME}")
                set(QM_FILE "${bin_qrc_dir}/${LOCAL_LANGUAGE_NAME}.qm")

                add_custom_target(${QM_TARGET_NAME}
                        COMMAND ${LRELEASE_PATH} ${TS_FILE} -qm ${QM_FILE}
                        VERBATIM)

                # 因为得先有ts文件才能生成qm文件,所以把构建ts文件的目标作为自己的依赖
                add_dependencies(${QM_TARGET_NAME} ${TS_TARGET_NAME})
                add_dependencies(${create_all_qm_files} ${QM_TARGET_NAME})


                MESSAGE("aaa=${create_all_qm_files}")
                IF (NOT EXISTS ${QM_FILE})
                        message("a=${TS_FILE}")
                        # message("b=${}")
                endif()

        endforeach()
endmacro( )

宏解释

  • target_name - 哪个项目
  • qt_bin_dir - qt的安装目录,用于寻找 lupdate 和 lrease, 以windows为例,我这里传递qt_bin_dir, 指向本机Qt的安装路径
set(CMAKE_PREFIX_PATH "C:/major/development/tools/qt/5.14/install/5.14.2/msvc2015_64")
set(qt_bin_dir
  ${CMAKE_PREFIX_PATH}/bin
)
  • TS_FILES - 项目的TS文件列表
  • bin_qrc_dir - 编译目录下的qrc文件的路径, 参考中将qrc拷贝到了 build 目录下
  • update_src_dir - 指向当需要更新ts文件的目录

使用范例

_create_qm_file_(${PROJECT_NAME} ${qt_bin_dir} ${TS_FILES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src )
  • 根据生成的项目, 就能增量更新ts文件了,下面以VS项目为例,执行后的项目如下

一个完整的cmakelists.txt

cmake_minimum_required(VERSION 3.18)

project(cmake_demo01 LANGUAGES CXX)

set(CMAKE_PREFIX_PATH "C:/major/development/tools/qt/5.14/install/5.14.2/msvc2015_64")

set(qt_bin_dir
  ${CMAKE_PREFIX_PATH}/bin
)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)

find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)


# =========================================================
# 语言家文件
# =========================================================
set(TS_FILES ${CMAKE_SOURCE_DIR}/lang/zh_CN.ts)


macro(_create_qm_file_ target_name qt_bin_dir  TS_FILES bin_qrc_dir update_src_dir)
        set(update_all_ts_files ${target_name}_update_all_ts_files)
        set(create_all_qm_files ${target_name}_create_all_qm_files)

        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()

        foreach(TS_FILE ${TS_FILES})
                # 把zh_CN.ts中的zh_CN提取出来
                get_filename_component(LOCAL_LANGUAGE_NAME ${TS_FILE} NAME_WE)
                set(TS_TARGET_NAME "${target_name}_update_ts_file_${LOCAL_LANGUAGE_NAME}")
                add_custom_target(${TS_TARGET_NAME}    COMMAND ${LUPDATE_PATH} ${update_src_dir} -ts ${TS_FILE} VERBATIM)

                # 将update_ts_file_<NAME>添加为update_all_ts_files的依赖,下同
                add_dependencies(${update_all_ts_files} ${TS_TARGET_NAME})

                set(QM_TARGET_NAME "${target_name}_create_qm_file_${LOCAL_LANGUAGE_NAME}")
                set(QM_FILE "${bin_qrc_dir}/${LOCAL_LANGUAGE_NAME}.qm")

                add_custom_target(${QM_TARGET_NAME}  COMMAND ${LRELEASE_PATH} ${TS_FILE} -qm ${QM_FILE}    VERBATIM)

                # 因为得先有ts文件才能生成qm文件,所以把构建ts文件的目标作为自己的依赖
                add_dependencies(${QM_TARGET_NAME} ${TS_TARGET_NAME})
                add_dependencies(${create_all_qm_files} ${QM_TARGET_NAME})
        endforeach()
endmacro( )
# =========================================================

set(bin_trans_qrc trans.qrc)
configure_file(${bin_trans_qrc} ${CMAKE_CURRENT_BINARY_DIR})

file(GLOB_RECURSE src_file ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE inc_file ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)

add_executable(${PROJECT_NAME} ${src_file}  ${inc_file}  ${CMAKE_CURRENT_BINARY_DIR}/trans.qrc)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(cmake_demo01 PRIVATE Qt5::Widgets Qt5::Core Qt5::Gui)

_create_qm_file_(${PROJECT_NAME} ${qt_bin_dir} ${TS_FILES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src )



这篇关于cmake之Qt国际化的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程