UI 上未显示工程英语

Engineering English not showing on UI

这是我的 QML 代码:

Text {
     id: helloText
     //: Hello World text
     //% "Hello World"
     //~ Context No specific context
     text: qsTrId("hello-world-id");
     anchors.top: parent.top
}

摘自 Qt 文档 here:

The "Engineering English" text that you see in the user interface for development builds is indicated with a //% comment. If you do not include this, the text ID will be shown in the user interface.

当我 运行 应用程序时,我看到 hello-world-id 而不是 Hello World,如文档中所述。我在这里遗漏了什么吗?

提前致谢。

首先,您必须创建 .ts 包含已翻译字符串的文件。例如,如果我想为俄语语言环境创建翻译,我会这样做:

lupdate main.qml -ts your_project_ru_RU.ts

如果您有多个 QML 文件,您可以在 .pro 文件中指定:

lupdate_only {
    SOURCES = main.qml \
              foo.qml
}

这样写:

lupdate your_project.pro -ts your_project_ru_RU.ts

此命令将为俄语区域设置创建未翻译的文件,您必须使用 QT Linguist 打开该文件才能翻译。

您可以在 .pro 文件中指定 .ts 个文件:

TRANSLATIONS += your_project_ru_RU.ts

毕竟你必须编译文件(Qt Linguist 中的File/Compile)。这将创建 your_project_ru_RU.qm.

最后,您必须加载 .qm 文件,例如 main.cpp:

QTranslator translator;    
translator.load("your_project_" + QLocale::system().name());
app.installTranslator(&translator);

更新:

如果您需要一些 Qt i18n 引擎的外观,而不提供 qm 个文件:

foo.qml

import QtQuick 2.4
import QtQuick.Window 2.2
import "i18n.js" as I18n

Window {
    Text {
        text: I18n.myTr("text-id");
        anchors.centerIn: parent
    }
}

i18n.js

var texts = {
    "text-id": "Hello, World!",
    "another-text-id": "Goodbye, World!"
}

function myTr(textid) {
    return texts[textid] ? texts[textid] : "";
}
  1. 通过 lupdate 创建了一个 .ts 文件:

    lupdate main.qml -ts your_project_ru_RU.ts

  2. 在 Qt 语言学家中打开 .ts 文件并添加您的翻译
  3. 在 lrelease 工具中调用 -idbased 选项:

    lrelease -idbased your_project_ru_RU.ts

访问http://doc.qt.io/qt-5/qml-qtqml-qt.html#qsTrId-method