From 3cc40c44aad39216c0065fb77255be1a1074920c Mon Sep 17 00:00:00 2001 From: jared Date: Thu, 27 Aug 2020 09:49:24 +0800 Subject: [PATCH] Trans use qsTr. --- examples/TaoQuickShow/Qml/Page/AboutDialog.qml | 2 +- examples/TaoQuickShow/Qml/Page/Biz/CompCard.qml | 2 +- examples/TaoQuickShow/Qml/Page/Biz/LangBtn.qml | 2 +- examples/TaoQuickShow/Qml/Page/Biz/SkinBtn.qml | 2 +- examples/TaoQuickShow/Qml/Page/ContentPage.qml | 4 ++-- examples/TaoQuickShow/Qml/Page/MenuPage.qml | 4 ++-- examples/TaoQuickShow/Qml/Page/NotifyBox.qml | 2 +- examples/TaoQuickShow/Src/Trans.cpp | 15 +++++++++++++-- examples/TaoQuickShow/Src/Trans.h | 5 ++++- examples/TaoQuickShow/Src/main.cpp | 1 + examples/TaoQuickShow/TaoQuickShow.pri | 6 ++++++ examples/TaoQuickShow/TaoQuickShow.pro | 5 ----- 12 files changed, 33 insertions(+), 17 deletions(-) diff --git a/examples/TaoQuickShow/Qml/Page/AboutDialog.qml b/examples/TaoQuickShow/Qml/Page/AboutDialog.qml index f54c1c6..3b2b206 100644 --- a/examples/TaoQuickShow/Qml/Page/AboutDialog.qml +++ b/examples/TaoQuickShow/Qml/Page/AboutDialog.qml @@ -30,7 +30,7 @@ Popup { } TTextBtn { id: closeBtn - text: trans.trans("Close") + trans.transString + text: qsTr("Close") width: 80 height: 38 radius: 8 diff --git a/examples/TaoQuickShow/Qml/Page/Biz/CompCard.qml b/examples/TaoQuickShow/Qml/Page/Biz/CompCard.qml index 84f2cfa..d9f0303 100644 --- a/examples/TaoQuickShow/Qml/Page/Biz/CompCard.qml +++ b/examples/TaoQuickShow/Qml/Page/Biz/CompCard.qml @@ -50,7 +50,7 @@ Rectangle { ToolTip { id: tip delay: 800 - text: trans.trans(String("Component Name: %1, Count: %2")).arg(trans.trans(name)).arg(count) + trans.transString + text: qsTr(String("Component Name: %1, Count: %2")).arg(qsTr(name)).arg(count) visible: area.containsMouse } MouseArea { diff --git a/examples/TaoQuickShow/Qml/Page/Biz/LangBtn.qml b/examples/TaoQuickShow/Qml/Page/Biz/LangBtn.qml index 5f1f0af..8e04c45 100644 --- a/examples/TaoQuickShow/Qml/Page/Biz/LangBtn.qml +++ b/examples/TaoQuickShow/Qml/Page/Biz/LangBtn.qml @@ -23,7 +23,7 @@ TImageBtn { delegate: TTextBtn { width: langListView.width height: 36 - text: trans.trans(modelData) + trans.transString + text: qsTr(modelData) color: trans.currentLang === modelData ? gConfig.themeColor :( containsMouse ? "lightgray" : pop.barColor) textColor: gConfig.textColor radius: 4 diff --git a/examples/TaoQuickShow/Qml/Page/Biz/SkinBtn.qml b/examples/TaoQuickShow/Qml/Page/Biz/SkinBtn.qml index b0da971..e58e47f 100644 --- a/examples/TaoQuickShow/Qml/Page/Biz/SkinBtn.qml +++ b/examples/TaoQuickShow/Qml/Page/Biz/SkinBtn.qml @@ -42,7 +42,7 @@ TImageBtn { centerIn: parent } color: "white" - text: trans.trans(model.name) + trans.transString + text: qsTr(model.name) } Rectangle { x: parent.width - width diff --git a/examples/TaoQuickShow/Qml/Page/ContentPage.qml b/examples/TaoQuickShow/Qml/Page/ContentPage.qml index 4e74d41..e9b7298 100644 --- a/examples/TaoQuickShow/Qml/Page/ContentPage.qml +++ b/examples/TaoQuickShow/Qml/Page/ContentPage.qml @@ -15,7 +15,7 @@ Background { topMargin: 60 } property string title: defaultTitle - text: trans.trans(title) + trans.transString + text: qsTr(title) } GridView { id: gridView @@ -34,7 +34,7 @@ Background { height: 100 CompCard { anchors.centerIn: parent - name: trans.trans(modelData.name) + trans.transString + name: qsTr(modelData.name) count: modelData.count icon: modelData.icon onClicked: { diff --git a/examples/TaoQuickShow/Qml/Page/MenuPage.qml b/examples/TaoQuickShow/Qml/Page/MenuPage.qml index c70a196..be960c3 100644 --- a/examples/TaoQuickShow/Qml/Page/MenuPage.qml +++ b/examples/TaoQuickShow/Qml/Page/MenuPage.qml @@ -29,7 +29,7 @@ ListView { id: btn width: root.width height: rowHeight - text: trans.trans(model.name) + trans.transString + text: qsTr(model.name) textItem.leftPadding: 6 textHorizontalAlignment: Text.AlignLeft textColor: text === currentTitle ? gConfig.titleBackground : gConfig.textColor @@ -68,7 +68,7 @@ ListView { delegate: TGradientBtn { width: root.width height: rowHeight - text: trans.trans(model.name) + trans.transString + text: qsTr(model.name) textColor: text === currentTitle ? gConfig.titleBackground : gConfig.textColor onClicked: { currentTitle = model.name diff --git a/examples/TaoQuickShow/Qml/Page/NotifyBox.qml b/examples/TaoQuickShow/Qml/Page/NotifyBox.qml index 01e46fc..7acdaa2 100644 --- a/examples/TaoQuickShow/Qml/Page/NotifyBox.qml +++ b/examples/TaoQuickShow/Qml/Page/NotifyBox.qml @@ -45,7 +45,7 @@ Rectangle { } function notify(msg) { - t.text = msg + trans.transString + t.text = msg ani.start() } } diff --git a/examples/TaoQuickShow/Src/Trans.cpp b/examples/TaoQuickShow/Src/Trans.cpp index 867aaea..2f74e16 100644 --- a/examples/TaoQuickShow/Src/Trans.cpp +++ b/examples/TaoQuickShow/Src/Trans.cpp @@ -7,7 +7,7 @@ const static auto cEnglisthStr = QStringLiteral("English"); const static auto cChineseStr = QStringLiteral("简体中文"); Trans::Trans(QObject* parent) - : QObject(parent) + : QTranslator(parent) { } void Trans::init() {} @@ -17,11 +17,22 @@ void Trans::uninit() {} void Trans::beforeUiReady(QQmlContext* ctx) { ctx->setContextProperty("trans", this); + loadFolder(qApp->applicationDirPath() + "/Trans"); + qApp->installTranslator(this); } void Trans::afterUiReady() { - loadFolder(qApp->applicationDirPath() + "/Trans"); + +} + +QString Trans::translate(const char *context, const char *sourceText, const char *disambiguation, int n) const +{ + Q_UNUSED(context) + Q_UNUSED(disambiguation) + Q_UNUSED(n) + + return trans(sourceText); } void Trans::loadFolder(const QString& folder) diff --git a/examples/TaoQuickShow/Src/Trans.h b/examples/TaoQuickShow/Src/Trans.h index 4496933..9d9a979 100644 --- a/examples/TaoQuickShow/Src/Trans.h +++ b/examples/TaoQuickShow/Src/Trans.h @@ -5,7 +5,8 @@ #include #include #include "TaoObject.h" -class Trans : public QObject, public TaoObject +#include +class Trans : public QTranslator, public TaoObject { Q_OBJECT Q_PROPERTY(QString currentLang READ currentLang WRITE setCurrentLang NOTIFY currentLangChanged) @@ -25,6 +26,8 @@ public: void beforeUiReady(QQmlContext* ctx) override; void afterUiReady() override; + + QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const override; public: const QString ¤tLang() const; diff --git a/examples/TaoQuickShow/Src/main.cpp b/examples/TaoQuickShow/Src/main.cpp index aa350d1..5234663 100644 --- a/examples/TaoQuickShow/Src/main.cpp +++ b/examples/TaoQuickShow/Src/main.cpp @@ -37,6 +37,7 @@ int main(int argc, char** argv) TaoFramework::instance()->beforeUiReady(view.rootContext()); view.engine()->addImportPath(qmlPath); + QObject::connect(TaoFramework::instance()->getObject(), &Trans::currentLangChanged, view.engine(), &QQmlEngine::retranslate); view.rootContext()->setContextProperty("qmlPath", qmlPath); view.rootContext()->setContextProperty("imgPath", imgPath); view.rootContext()->setContextProperty("contentsPath", contentsPath); diff --git a/examples/TaoQuickShow/TaoQuickShow.pri b/examples/TaoQuickShow/TaoQuickShow.pri index 107b05f..dc5221a 100644 --- a/examples/TaoQuickShow/TaoQuickShow.pri +++ b/examples/TaoQuickShow/TaoQuickShow.pri @@ -1,5 +1,11 @@ CONFIG += file_copies +#一部分头文件加入编译预处理,提高编译速度 +msvc | gcc | xcode { + CONFIG += precompile_header + PRECOMPILED_HEADER = $$PWD/Src/stdafx.h +} + HEADERS += \ $$PWD/Src/TaoObject.h \ $$PWD/Src/TaoFramework.h \ diff --git a/examples/TaoQuickShow/TaoQuickShow.pro b/examples/TaoQuickShow/TaoQuickShow.pro index 0a5bf9d..484aecb 100644 --- a/examples/TaoQuickShow/TaoQuickShow.pro +++ b/examples/TaoQuickShow/TaoQuickShow.pro @@ -31,11 +31,6 @@ CONFIG(debug,debug|release) { # QMAKE_CFLAGS += -source-charset:utf-8 # QMAKE_CXXFLAGS += -source-charset:utf-8 #} -#一部分头文件加入编译预处理,提高编译速度 -msvc | gcc | xcode { - CONFIG += precompile_header - PRECOMPILED_HEADER = $$PWD/Src/stdafx.h -} include($${TaoQuick_3RDPARTY_TREE}/3rdparty.pri)