QT6.3 CMake 多语言切换

网上很多方法,总是不成功
要么提示:lupdate warning: no TS files specified. Only diagnostics will be produced.
要么文件为空。
解决办法参考 QT6.3 CMake Linguist多语言配置记录

1、CMakeLists.txt文件中添加LinguistTools

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools)

2、

qt_add_lupdate(untitled TS_FILES  untitled_ch.ts)
qt_add_lrelease(untitled
    TS_FILES untitled_ch.ts
    QM_FILES_OUTPUT_VARIABLE qm_files)
qt_add_resources(untitled "translations"
    PREFIX "/"
    BASE "${CMAKE_CURRENT_BINARY_DIR}"
    FILES "${qm_files}")

或者

qt_add_translations(untitled TS_FILES untitled_ch.ts)

多个ts文件时

#qt_add_translations(untitled TS_FILES untitled_ch.ts untitled_en.ts)
qt_add_lupdate(untitled TS_FILES  untitled_ch.ts untitled_en.ts)
qt_add_lrelease(untitled
    TS_FILES untitled_ch.ts untitled_en.ts
    QM_FILES_OUTPUT_VARIABLE qm_files)
qt_add_resources(untitled "translations"
    PREFIX "/"
    BASE "${CMAKE_CURRENT_BINARY_DIR}"
    FILES "${qm_files}")

生成后,自动生成文件ts文件
在这里插入图片描述

ts文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
</TS>

方法一
项目->Build->Build的步骤->详情
在这里插入图片描述
勾选 lrelease和lupdate
在这里插入图片描述
重新生成后,ts里面有内容了。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
<context>
    <name>MainWindow</name>
    <message>
        <location filename="mainwindow.ui" line="14"/>
        <location filename="../build-untitled-Desktop_Qt_6_3_1_MinGW_64_bit-Debug/untitled_autogen/include/ui_mainwindow.h" line="60"/>
        <source>MainWindow</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.ui" line="27"/>
        <location filename="mainwindow.ui" line="40"/>
        <location filename="../build-untitled-Desktop_Qt_6_3_1_MinGW_64_bit-Debug/untitled_autogen/include/ui_mainwindow.h" line="61"/>
        <location filename="../build-untitled-Desktop_Qt_6_3_1_MinGW_64_bit-Debug/untitled_autogen/include/ui_mainwindow.h" line="62"/>
        <source>PushButton</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="9"/>
        <source>中文</source>
        <translation type="unfinished"></translation>
    </message>
    <message>
        <location filename="mainwindow.cpp" line="10"/>
        <source>英文</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

将ts文件加载到项目中,用Qt Linguist打开
在这里插入图片描述
设置语言
在这里插入图片描述
根据需求编辑语言
在这里插入图片描述

最后,加载qm文件使用

完整版 CMakeLists.txt 如下

cmake_minimum_required(VERSION 3.5)

project(untitled VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

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

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools)


set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        untitled_ch.ts
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(untitled
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET untitled APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(untitled SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(untitled
            ${PROJECT_SOURCES}
        )
    endif()
endif()

#qt_add_translations(untitled TS_FILES untitled_ch.ts)
qt_add_lupdate(untitled TS_FILES  untitled_ch.ts)
qt_add_lrelease(untitled
    TS_FILES untitled_ch.ts
    QM_FILES_OUTPUT_VARIABLE qm_files)
qt_add_resources(untitled "translations"
    PREFIX "/"
    BASE "${CMAKE_CURRENT_BINARY_DIR}"
    FILES "${qm_files}")


target_link_libraries(untitled PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

set_target_properties(untitled PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(untitled)
endif()


参考 QT6.3 CMake Linguist多语言配置记录

方法二 (补充)
在CMakeLists.txt里面加如下代码,ts文件会自动填充

add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_lupdate)
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_lrelease)

参考Qt with CMake: Translation .ts files are not updated

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值