本页内容

C

Qt Quick Ultralite 翻译示例

演示如何在Qt Quick Ultralite应用程序中实现翻译功能。

概述

该translation 示例采用了“Qt Quick Ultralite的国际化和本地化”主题中所述的技术。它演示了如何将本地化资源嵌入到应用程序中,以及如何利用现有的QML API在运行时更改UI语言。

该翻译示例展示了应用程序的用户界面,界面采用英语(美国)版本,带有美国国旗和英文文本。

翻译示例,展示了带有日本国旗和日文文本的日语版应用程序界面。

该翻译示例展示了应用程序的用户界面,界面采用阿拉伯语,配有沙特阿拉伯国旗和阿拉伯语文本。

通过轻触屏幕,您可以在可用语言(英语、挪威语、拉脱维亚语、日语、阿拉伯语和泰语)之间切换。更改应用程序语言会更新国旗图像以及“Text ”和“StaticText ”项目。只有StaticText 能通过静态字体引擎正确渲染复杂脚本。

启用“文本省略”选项后的挪威语翻译示例,显示了被截断的文本项内容,其背景为粉色,用于标记省略的边界。

该应用程序提供以下选项:

  • Elide text - 仅对“文本”控件中的文本进行截断。StaticText 控件不受此选项影响,因为该控件不支持文本截断。
  • Show text background - 为文本项添加粉色背景,以便直观显示文本将在哪些边界处被截断。

目标平台

项目结构

CMake 项目文件

本示例的CMakeLists.txt文件定义了三个独立的 CMake 目标:

  • translation_all - 构建包含所有翻译的应用程序。
  • translation_lv - 仅使用拉脱维亚语翻译构建应用程序。
  • translation_spark - 构建包含所有翻译以及Monotype Spark 字体引擎的应用程序。
QmlProject 文件

由于应用程序使用的文本包含Qt Quick Ultralite默认字体中未提供的字形,因此每个目标中都向项目添加了自定义字体。

    // Qul ships some default fonts. Since they don't contain glyphs for
    // all the characters that are needed for this application,
    // add some extra fonts.
    FontFiles {
        files: [
            "fonts/kosugi/Kosugi-Regular.ttf",
            "fonts/sarabun/Sarabun-Regular.ttf"
        ]
    }
translation_all

此应用程序变体包含所有可用的翻译。

    TranslationFiles {
        files: [
            "translation.ar_SA.ts",
            "translation.ja_JP.ts",
            "translation.lv_LV.ts",
            "translation.nb_NO.ts",
            "translation.th_TH.ts"
        ]
    }

    // Qul ships some default fonts. Since they don't contain glyphs for
translation_lv

在此,仅提供拉脱维亚语,并将其设为应用程序的默认语言。

    TranslationFiles {
        files: ["translation.lv_LV.ts"]
        MCU.omitSourceLanguage: true
    }
translation_spark

此应用程序变体与translation_all 几乎完全相同,只是它使用的是 Monotype Spark 字体渲染引擎。

...
        TranslationFiles {
                files: [
                "translation.ar_SA.ts",
                "translation.ja_JP.ts",
                "translation.lv_LV.ts",
                "translation.nb_NO.ts",
                "translation.th_TH.ts"
                ]
        }

        FontFiles {
                files: [
                        "fonts/monotype/TranslationsSampleFontmap.fmp"
                ]
        }

        MCU.Config {
                fontEngine: "Spark"
                defaultFontFamily: "arabic-style-font"
                fontCacheSize: 1 // Disable the cache
        }
...
应用程序 UI

translation.qml文件定义了应用程序的用户界面。

要标记可本地化的字符串,请使用qStr() 方法。

StaticText {
    width: root.textMaxWidth
    text: qsTr("orange", "color")
}
StaticText {
    width: root.textMaxWidth
    text: qsTr("orange", "fruit")
}
StaticText {
    width: root.textMaxWidth
    text: qsTr("sunrise")
}
StaticText {
    // This string is not translated in the .ts files and
    // will just fall back to the source string.
    width: root.textMaxWidth
    text: qsTr("hello")
}

Qt.uiLanguage 属性是处理翻译时最需要关注的焦点。

如果应用程序仅使用一种可用语言构建,则Qt.uiLanguage 属性将为空。

// Disable language switching when this is compiled with
// a single language.
Component.onCompleted: allowLangSwitch = (Qt.uiLanguage == "")

更改Qt.uiLanguage 属性的值即可更改当前 UI 的显示语言。

MouseArea {
    anchors.fill: parent
    onClicked: {
        if (!root.allowLangSwitch)
            return

        var lang = Qt.uiLanguage
        switch (lang) {
            case "nb_NO": lang = "lv_LV"; break;
            case "lv_LV": lang = "ja_JP"; break;
            case "ja_JP": lang = "ar_SA"; break;
            case "ar_SA": lang = "th_TH"; break;
            case "th_TH": lang = ""; break;
            default: lang = "nb_NO"; break;
        }
        Qt.uiLanguage = lang
    }
}

注意:设置为 空字符串意味着使用“默认”语言(卸载.ts 文件),此时将使用包裹在qStr() 中的原始文本。

特定于翻译的图像

Text.StyledText qsTr() 支持格式化字符串。这种方法使翻译人员能够根据翻译字符串中的词序,灵活地更改文本中图片的位置。

StaticText {
    width: root.width
    topPadding: 8
    textFormat: Text.StyledText
    text: qsTr("English language (US) <img src=\"usa-flag-small-icon.png\" width=\"38\" height=\"20\">")
}

或者,可以使用Image 项,并将source 属性绑定到Qt.uiLanguage 上。

Image {
    readonly property bool horizontalScreen: root.width > root.height
    width: horizontalScreen ? root.width / 4 : root.width / 3
    height: horizontalScreen ? root.height / 4 : root.height / 8
    // If this application is build with, for example, only one translation
    // (see translation_lv.qmlproject), then only one of flag PNGs
    // is needed in the binary (registered with the resouce system).
    // By using a function here we prevent qmltocpp from trying to
    // resolve file paths to images at build-time. Instead
    // a code is generated that will try to resolve the path to
    // image at run-time.
    source: flagForLanguage()
}

翻译文件(.ts)

当应用程序首次构建时,.ts 文件会生成在项目的根目录中。

翻译文件是一个纯文本 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="nb_NO">
<context>
    <name>translation</name>
    <message>
        <location filename="translation.qml" line="108"/>
        <source>English language (US) &lt;img src=&quot;usa-flag-small-icon.png&quot; width=&quot;38&quot; height=&quot;20&quot;&gt;</source>
        <oldsource>English language (US) &lt;img src=&quot;usa-flag-small-icon.png&quot;&gt; width=&quot;38&quot; height=&quot;20&quot;</oldsource>
        <translation>Norsk språk &lt;img src=&quot;norway-flag-small-icon.png&quot; width=&quot;27&quot; height=&quot;20&quot;&gt;</translation>
    </message>
    ...

您可以使用任何文本/XML 编辑器为<translation> 节点提供正确的值。 Qt Linguist GUI 编辑器也是处理翻译文件的绝佳选择。

触发自动生成的update_translations CMake 目标的构建,以根据源代码中新增和已修改的可翻译字符串来创建或更新.ts 文件。

编辑完 `.ts ` 文件后,请重新构建应用程序目标以更新翻译。

如果翻译文件中未找到用qStr() 包裹的字符串,则将使用原始(源)字符串。

文件:

图片:

另请参阅 《使用Qt Quick Ultralite 进行国际化和本地化》。

在某些 Qt 许可下可用。
了解更多。