Use Qt. locale to display local data, and use qt. locale to localize

Source: Internet
Author: User

Use Qt. locale to display local data, and use qt. locale to localize

We know that for some applications, we can display different data formats, such as time and money, based on the language selection. In today's routine, we will show you how to use Qt. locale to display data in different formats based on different languages.

 

 

import QtQuick 2.0import Ubuntu.Components 1.1/*!    \brief MainView with a Label and Button elements.*/MainView {    // objectName for functional testing purposes (autopilot-qt5)    objectName: "mainView"    // Note! applicationName needs to match the "name" field of the click manifest    applicationName: "locale.liu-xiao-guo"    /*     This property enables the application to change orientation     when the device is rotated. The default is false.    */    //automaticOrientation: true    // Removes the old toolbar and enables new features of the new header.    useDeprecatedToolbar: false    width: units.gu(60)    height: units.gu(85)    Page {        title: i18n.tr("Locale")        Rectangle {            id: root            anchors.fill: parent            color: "lightgray"            property string locale: view.currentItem.locale            Text {                id: title                text: "Select locale:"            }            Rectangle {                id: chooser                anchors.top: title.bottom                anchors.topMargin: 5                width: parent.width-10                x: 5                height: parent.height/2 - 10                color: "#40300030"                ListView {                    id: view                    clip: true                    focus: true                    anchors.fill: parent                    model: [                        "en_US",                        "en_GB",                        "fi_FI",                        "de_DE",                        "ar_SA",                        "hi_IN",                        "zh_CN",                        "th_TH",                        "fr_FR",                        "nb_NO",                        "sv_SE"                    ]                    delegate: Text {                        property string locale: modelData                        height: units.gu(3)                        width: view.width                        text: Qt.locale(modelData).name + " ("+ Qt.locale(modelData).nativeCountryName + "/" + Qt.locale(modelData).nativeLanguageName + ")"                        MouseArea {                            anchors.fill: parent                            onClicked: view.currentIndex = index                        }                    }                    highlight: Rectangle {                        height: 30                        color: "#60300030"                    }                }            }            Rectangle {                color: "white"                anchors.top: chooser.bottom                anchors.topMargin: 5                anchors.bottom: parent.bottom                anchors.bottomMargin: 5                x: 5; width: parent.width - 10                Column {                    anchors.fill: parent                    spacing: 5                    Text {                        property var date: new Date()                        text: "Date: " + date.toLocaleDateString(Qt.locale(root.locale))                    }                    Text {                        property var date: new Date()                        text: "Time: " + date.toLocaleTimeString(Qt.locale(root.locale))                    }                    Text {                        property var dow: Qt.locale(root.locale).firstDayOfWeek                        text: "First day of week: " + Qt.locale(root.locale).standaloneDayName(dow)                    }                    Text {                        property var num: 10023823                        text: "Number: " + num.toLocaleString(Qt.locale(root.locale))                    }                    Text {                        property var num: 10023823                        text: "Currency: " + num.toLocaleCurrencyString(Qt.locale(root.locale))                    }                }            }        }    }}

 

 

In the routine, note the following syntax:

 

 

delegate: Text {                        property string locale: modelData                        height: units.gu(3)                        width: view.width                        text: Qt.locale(modelData).name + " ("+ Qt.locale(modelData).nativeCountryName + "/" + Qt.locale(modelData).nativeLanguageName + ")"                        MouseArea {                            anchors.fill: parent                            onClicked: view.currentIndex = index                        }                    }

We can get the modelData of the current Item by defining a property locale. In this way, we use

 

 

 

 property string locale: view.currentItem.locale

To get the modelData in the current list, and then reference it in other parts of the program!
 

 

 

Source code for the entire project in: git clone https://gitcafe.com/ubuntu/locale.git

 

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.