为什么我的QT5语言翻译加载?

发布于 2025-01-17 20:57:54 字数 3710 浏览 4 评论 0原文

我正在尝试使用 CMake 编写一个带有语言翻译的简单 qml 应用程序,尽管该应用程序运行,但它从不显示翻译。我使用的是 CMake 版本 3.22.2 和 Qt 版本 3.15.2 的 Linux 平台,这是目录结构:

├── CMakeLists.txt
└── src
    ├── CMakeLists.txt
    ├── main.cpp
    ├── silly.qrc
    └── qml
        ├── silly.qml
        └── translations
            ├── qml_de.ts
            ├── qml_en.ts
            └── qml_fr.ts

CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
project(silly VERSION 1.0.0)
add_subdirectory(src)

src/CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
find_package(Qt5 COMPONENTS Qml Quick LinguistTools REQUIRED)
set(TRANSLATIONS 
    qml/translations/qml_en.ts
    qml/translations/qml_fr.ts
    qml/translations/qml_de.ts
)
qt5_create_translation(QM_FILES ${CMAKE_CURRENT_SOURCE_DIR} ${TRANSLATIONS})
qt5_add_resources(QRC_RESOURCES "silly.qrc") 

add_executable(silly main.cpp 
    "${QRC_RESOURCES}"
    "${QM_FILES}"
)
target_compile_features(silly PUBLIC cxx_std_17)
set_target_properties(silly PROPERTIES AUTOMOC ON AUTORCC ON)
target_link_libraries(silly PRIVATE Qt5::Quick Qt5::Qml)
target_include_directories(silly
    PUBLIC
        $<INSTALL_INTERFACE:.>
        $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
)
install(TARGETS silly)

src/main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.addImportPath("qrc:/");
    engine.load(QUrl("qrc:/qml/silly.qml"));

    return app.exec();
}

src/silly.qrc

<!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="/qml">
        <file alias="silly.qml">qml/silly.qml</file>
    </qresource>
</RCC>

src/qml/silly.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true

    Button {
        anchors.fill: parent
        spacing: 20
        text: qsTr("Hello")
    }
}

src/qml/translations/qml_de.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="de_DE">
<context>
    <name>silly</name>
    <message>
        <location filename="../silly.qml" line="11"/>
        <source>Hello</source>
        <translation>Hallo</translation>
    </message>
</context>
</TS>

src/qml/translations/qml_en.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
    <name>silly</name>
    <message>
        <location filename="../silly.qml" line="11"/>
        <source>Hello</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

src/qml/translations/qml_fr.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="fr_FR">
<context>
    <name>silly</name>
    <message>
        <location filename="../silly.qml" line="11"/>
        <source>Hello</source>
        <translation>Bonjour</translation>
    </message>
</context>
</TS>

它构建并运行没有错误,但是当我尝试用它进行测试时: LANGUAGE=fr src/silly 我得到一个显示“Hello”的按钮,而不是显示“Bonjour”的按钮。我花了几个小时试图解决这个问题,这也让我想到了第二个问题:通常如何解决 Qt 语言翻译问题?这是我第一次,我找不到任何相关文档。

I am trying to write a simple qml application with language translations using CMake and although the application runs, it never shows translations. I'm on a Linux platform with CMake version 3.22.2 and Qt version 3.15.2 Here is the directory structure:

├── CMakeLists.txt
└── src
    ├── CMakeLists.txt
    ├── main.cpp
    ├── silly.qrc
    └── qml
        ├── silly.qml
        └── translations
            ├── qml_de.ts
            ├── qml_en.ts
            └── qml_fr.ts

CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
project(silly VERSION 1.0.0)
add_subdirectory(src)

src/CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
find_package(Qt5 COMPONENTS Qml Quick LinguistTools REQUIRED)
set(TRANSLATIONS 
    qml/translations/qml_en.ts
    qml/translations/qml_fr.ts
    qml/translations/qml_de.ts
)
qt5_create_translation(QM_FILES ${CMAKE_CURRENT_SOURCE_DIR} ${TRANSLATIONS})
qt5_add_resources(QRC_RESOURCES "silly.qrc") 

add_executable(silly main.cpp 
    "${QRC_RESOURCES}"
    "${QM_FILES}"
)
target_compile_features(silly PUBLIC cxx_std_17)
set_target_properties(silly PROPERTIES AUTOMOC ON AUTORCC ON)
target_link_libraries(silly PRIVATE Qt5::Quick Qt5::Qml)
target_include_directories(silly
    PUBLIC
        
lt;INSTALL_INTERFACE:.>
        
lt;BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
)
install(TARGETS silly)

src/main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.addImportPath("qrc:/");
    engine.load(QUrl("qrc:/qml/silly.qml"));

    return app.exec();
}

src/silly.qrc

<!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="/qml">
        <file alias="silly.qml">qml/silly.qml</file>
    </qresource>
</RCC>

src/qml/silly.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true

    Button {
        anchors.fill: parent
        spacing: 20
        text: qsTr("Hello")
    }
}

src/qml/translations/qml_de.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="de_DE">
<context>
    <name>silly</name>
    <message>
        <location filename="../silly.qml" line="11"/>
        <source>Hello</source>
        <translation>Hallo</translation>
    </message>
</context>
</TS>

src/qml/translations/qml_en.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
    <name>silly</name>
    <message>
        <location filename="../silly.qml" line="11"/>
        <source>Hello</source>
        <translation type="unfinished"></translation>
    </message>
</context>
</TS>

src/qml/translations/qml_fr.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="fr_FR">
<context>
    <name>silly</name>
    <message>
        <location filename="../silly.qml" line="11"/>
        <source>Hello</source>
        <translation>Bonjour</translation>
    </message>
</context>
</TS>

It builds and runs with no errors, but when I attempt to test with this:
LANGUAGE=fr src/silly I get a button that says "Hello" instead of a button that says "Bonjour". I've been trying to figure this out for hours which also leads me to the secondary question: how does one generally troubleshoot Qt language translations? This is my first time and I could find no documentation on that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2025-01-24 20:57:54

这是非常糟糕的文档记录,但它可以与使用专用 qmake 配置参数 embed_translations 的现成示例一起神奇地工作。我建议您查看原始示例的构建目录,其中生成了 .qm 文件和特殊的 qrc 文件 qmake_qmake_qm_files.qrc

除非您想支持动态语言切换,否则您不需要使用QTranslator。启动时,QML 运行时会自动从 i18n 子目录加载翻译文件 qml_.qm(qml_xx_XX.qm,其中 xx 是 ISO639,XX 是可选的 ISO 3166 代码)根 QML 文件的位置,基于系统语言(如果找到)。

您需要将 .qm 文件放入 qrc:/qml/i18n/ 文件夹,因为您的主 qml 文件位于 qrc:/qml/ 中。

使用CMake,您可以按如下方式执行此操作:

将新的 qrc 文件(例如 cmake_qm_files.qrc)添加到您的项目中

<RCC>
    <qresource prefix="/qml/i18n">
        <file>qml_de.qm</file>
        <file>qml_en.qm</file>
        <file>qml_fr.qm</file>
    </qresource>
</RCC>

获取 CMake 将 qrc 文件复制到 .qm 文件所在的二进制目录获取创建

configure_file(cmake_qm_files.qrc ${CMAKE_BINARY_DIR} COPYONLY)

编译 qrc 文件资源并将其嵌入到可执行文件中

add_executable(silly main.cpp
    ${QRC_RESOURCES}
    ${CMAKE_BINARY_DIR}/cmake_qm_files.qrc
)

我通常使用 QLocale 进行翻译测试,如下所示:

QGuiApplication app(argc, argv);

QLocale systemLocale = QLocale::system();
QLocale::Language language = systemLocale.language();
QLocale::Country country = systemLocale.country();
qDebug() << "System locale language:" << language << ", country:" << country;

// TEST: change default locale by removing comments below
// language = QLocale::French;
// country = QLocale::France;
language = QLocale::English;
country = QLocale::Australia;
QLocale locale(language, country);
qDebug() << "Changing default locale to language:" << locale.language() << ", country:" << locale.country();
QLocale::setDefault(locale); // TEST: set default locale to something else than system locale

QQmlApplicationEngine engine;

That's pretty badly documented stuff which works magically with ready-made example using dedicated qmake configuration parameter embed_translations. I advice you to take a look into the original example's build dir where .qm files and a special qrc file qmake_qmake_qm_files.qrc get generated.

You don't need to use QTranslator unless you want to support dynamic language switch. At startup, QML runtime automatically loads a translation file qml_<language_COUNTRY>.qm (qml_xx_XX.qm where xx is ISO639 and XX is optional ISO 3166 code) from the i18n subdirectory of the root QML file, based on the system language, if it finds one.

You need to get your .qm files to qrc:/qml/i18n/ folder because your main qml file is in qrc:/qml/.

With CMake you can do it as follows:

Add a new qrc file, e.g. cmake_qm_files.qrc to your project

<RCC>
    <qresource prefix="/qml/i18n">
        <file>qml_de.qm</file>
        <file>qml_en.qm</file>
        <file>qml_fr.qm</file>
    </qresource>
</RCC>

Get CMake to copy the qrc file to binary dir where .qm files get created

configure_file(cmake_qm_files.qrc ${CMAKE_BINARY_DIR} COPYONLY)

Get qrc file resource compiled and embedded to your executable

add_executable(silly main.cpp
    ${QRC_RESOURCES}
    ${CMAKE_BINARY_DIR}/cmake_qm_files.qrc
)

I typically use QLocale for translation testing as follows:

QGuiApplication app(argc, argv);

QLocale systemLocale = QLocale::system();
QLocale::Language language = systemLocale.language();
QLocale::Country country = systemLocale.country();
qDebug() << "System locale language:" << language << ", country:" << country;

// TEST: change default locale by removing comments below
// language = QLocale::French;
// country = QLocale::France;
language = QLocale::English;
country = QLocale::Australia;
QLocale locale(language, country);
qDebug() << "Changing default locale to language:" << locale.language() << ", country:" << locale.country();
QLocale::setDefault(locale); // TEST: set default locale to something else than system locale

QQmlApplicationEngine engine;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文