Label { id: helloLabel text = qsTr("Hello World") height: 50 anchors { top: parent.top left: parent.left right: parent.horizontalCenter margins: 10 }}
text: qsTr("Hello") + rootItem.emptyString
Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)
qApp->installTranslator
text: qsTr("Hello") + qmlTranslator.emptyString
#ifndef TRANSLATIONTEST_H#define TRANSLATIONTEST_H#include <QObject>#include <QtGui>class TranslationTest : public QObject{ Q_OBJECT Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)public: TranslationTest(); Q_INVOKABLE void selectLanguage(QString language); QString getEmptyString() const;signals: void languageChanged();private: QTranslator *translator1_; QTranslator *translator2_;};#endif // TRANSLATIONTEST_H
#include "translationtest.h"TranslationTest::TranslationTest(){ translator1_ = new QTranslator(this); translator2_ = new QTranslator(this);}void TranslationTest::selectLanguage(QString language){ if(language == QString("fr")) { translator1_->load("t1_fr", "."); qApp->installTranslator(translator1_); } if(language == QString("ru")) { translator2_->load("t1_ru", "."); qApp->installTranslator(translator2_); } if(language == QString("en")) { qApp->removeTranslator(translator1_); qApp->removeTranslator(translator2_); } emit languageChanged();}QString TranslationTest::getEmptyString() const{ return "";}
import QtQuick 2.7import QtQuick.Controls 1.5ApplicationWindow{ visible: true width: 640 height: 480 title: qsTr("Hello World") + rootItem.emptyString menuBar: MenuBar { Menu { title: qsTr("File") + rootItem.emptyString MenuItem { text: qsTr("&Open") + rootItem.emptyString onTriggered: console.log("Open action triggered"); } MenuItem { text: qsTr("Exit") + rootItem.emptyString onTriggered: Qt.quit(); } } } Rectangle { width: 340; height: 150 Column { anchors.fill: parent; spacing: 20 Text { text: qsTr("Hello") + rootItem.emptyString font.pointSize: 25; anchors.horizontalCenter: parent.horizontalCenter } } Row { anchors.verticalCenter: parent.verticalCenter Button { text: "English"; onClicked: rootItem.selectLanguage("en"); } Button { text: "Russian"; onClicked: rootItem.selectLanguage("ru"); } Button { text: "French"; onClicked: rootItem.selectLanguage("fr"); } } } Label { text: qsTr("Hello World") + rootItem.emptyString anchors.centerIn: parent }}
translator1_->load("t1_fr", ".");translator2_->load("t1_ru", ".");