C

Qt Quick Ultralite translation Example

/****************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/
import QtQuick 2.15 import QtQuickUltralite.Extras 2.0 import QtQuick.Controls 2.15 import fontconfiguration 1.0 Rectangle { id: root property int elideMode: Text.ElideNone property int textMaxWidth: root.width * 0.20 property color textsBackground: "transparent" readonly property font defaultFont: Qt.font({family: FontConfiguration.defaultFontFamily}) readonly property font arabicFont: Qt.font({family: FontConfiguration.arabicFontFamily}) readonly property font japaneseFont: Qt.font({family: FontConfiguration.japaneseFontFamily}) readonly property font thaiFont: Qt.font({family: FontConfiguration.thaiFontFamily}) function getFont() : font { var lang = Qt.uiLanguage switch (lang) { case "ja_JP": return japaneseFont; case "ar_SA": return arabicFont; case "th_TH": return thaiFont; default: return defaultFont; } } Column { spacing: -10 anchors.right: parent.right anchors.rightMargin: 20 anchors.top: parent.top anchors.topMargin: 33 // MouseArea fills the entire window, bring // the CheckBoxes to the front. z: 1 CheckBox { id: doElide checked: false text: "Elide text" onClicked: checked ? root.elideMode = Text.ElideRight : root.elideMode = Text.ElideNone } CheckBox { id: doBackground checked: false text: "Show text bounds" onClicked: checked ? root.textsBackground = "pink" : root.textsBackground = "transparent" } } Column { spacing: 14 Text { topPadding: 8 text: { var lang = "English (US)" switch (Qt.uiLanguage) { case "lv_LV": lang = "Latviešu"; break case "nb_NO": lang = "Norsk"; break case "ja_JP": lang = "日本語"; break case "ar_SA": lang = " العربية"; break case "th_TH": lang = "ไทย"; break } return " Current language: " + lang } } Row { spacing: 30 Item {} // dummy for spacing 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 source: { switch (Qt.uiLanguage) { case "lv_LV": return "latvia-flag-small.png" case "nb_NO": return "norway-flag-small.png"; case "ja_JP": return "japan-flag-small.png"; case "ar_SA": return "saudi-arabia-flag-small.png"; case "th_TH": return "thai-flag-small.png"; default: return "usa-flag-small.png"; } } } } Row { spacing: 30 // dummy item for spacing at the beginning Item {} Column { Text { id: staticTextItemsTitle text: "StaticText items" } Rectangle { height: staticTextColumn.height width: root.textMaxWidth color: root.textsBackground // Currently StaticText items support implicit text alignment with // Spark font engine only. For RTL language the UI is neater if we // anchor the right aligned texts at "parent.right". anchors.right: FontConfiguration.isSpark ? Qt.uiLanguage == "ar_SA" ? parent.right : undefined : undefined anchors.left: FontConfiguration.isSpark ? Qt.uiLanguage == "ar_SA" ? undefined : parent.left : undefined Column { id: staticTextColumn StaticText { width: root.textMaxWidth text: qsTr("orange", "color") } StaticText { width: root.textMaxWidth text: qsTr("orange", "fruit") } StaticText { width: root.textMaxWidth text: qsTr("sunrise") } StaticText { width: root.textMaxWidth text: qsTr("hello") } } } } Column { Text { text: "Text items" } Rectangle { height: textColumn.height width: root.textMaxWidth color: root.textsBackground Column { id: textColumn Text { width: root.textMaxWidth font: getFont() elide: root.elideMode text: qsTr("orange", "color") } Text { width: root.textMaxWidth font: getFont() elide: root.elideMode text: qsTr("orange", "fruit") } Text { width: root.textMaxWidth font: getFont() elide: root.elideMode text: qsTr("sunrise") } Text { width: root.textMaxWidth font: getFont() elide: root.elideMode // This string is not translated in the .ts files and // will just fall back to the source string. text: qsTr("hello") } } } } } } property bool allowLangSwitch : true // Disable language switching when this is compiled with // a single language. Component.onCompleted: allowLangSwitch = (Qt.uiLanguage == "") 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 } } }