File tree Expand file tree Collapse file tree 12 files changed +231
-0
lines changed Expand file tree Collapse file tree 12 files changed +231
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* !
2
+ *@file QmlLanguage.cpp
3
+ *@brief 语言切换
4
+ *@version 1.0
5
+ *@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6
+ *@author zhengtianzuo
7
+ */
8
+ #include " QmlLanguage.h"
9
+
10
+ QmlLanguage::QmlLanguage (QGuiApplication &app, QQmlApplicationEngine& engine)
11
+ {
12
+ m_app = &app;
13
+ m_engine = &engine;
14
+ }
15
+
16
+ void QmlLanguage::setLanguage (int nLanguage)
17
+ {
18
+ QTranslator translator;
19
+ if (nLanguage == 0 )
20
+ {
21
+ translator.load (" :/en_US.qm" );
22
+ }else {
23
+ translator.load (" :/zh_CN.qm" );
24
+ }
25
+ m_app->installTranslator (&translator);
26
+ m_engine->retranslate ();
27
+ }
Original file line number Diff line number Diff line change
1
+ /* !
2
+ *@file QmlLanguage.h
3
+ *@brief 语言切换
4
+ *@version 1.0
5
+ *@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6
+ *@author zhengtianzuo
7
+ */
8
+ #pragma once
9
+ #include < QObject>
10
+ #include < QTranslator>
11
+ #include < QGuiApplication>
12
+ #include < QQmlApplicationEngine>
13
+
14
+ class QmlLanguage : public QObject
15
+ {
16
+ Q_OBJECT
17
+ public:
18
+ QmlLanguage (QGuiApplication& app, QQmlApplicationEngine &engine);
19
+
20
+ Q_INVOKABLE void setLanguage (int nLanguage);
21
+
22
+ private:
23
+ QGuiApplication *m_app;
24
+ QQmlApplicationEngine *m_engine;
25
+ };
Original file line number Diff line number Diff line change
1
+ # -------------------------------------------------
2
+ #
3
+ # Copyright (C) 2003-2103 CamelSoft Corporation
4
+ #
5
+ # -------------------------------------------------
6
+
7
+ QT += qml quick
8
+
9
+ CONFIG += c++11
10
+
11
+ SOURCES += main.cpp \
12
+ QmlLanguage.cpp
13
+
14
+ RESOURCES += qml.qrc
15
+
16
+ TRANSLATIONS = zh_CN.ts en_US.ts
17
+
18
+ HEADERS += \
19
+ QmlLanguage.h
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" utf-8" ?>
2
+ <!DOCTYPE TS >
3
+ <TS version =" 2.1" language =" en" >
4
+ <context >
5
+ <name >main</name >
6
+ <message >
7
+ <location filename =" main.qml" line =" 15" />
8
+ <source >Dialog</source >
9
+ <translation >Dialog</translation >
10
+ </message >
11
+ <message >
12
+ <location filename =" main.qml" line =" 29" />
13
+ <source >TextLabel</source >
14
+ <translation >TextLabel</translation >
15
+ </message >
16
+ <message >
17
+ <location filename =" main.qml" line =" 36" />
18
+ <source >PushButton</source >
19
+ <translation >PushButton</translation >
20
+ </message >
21
+ <message >
22
+ <location filename =" main.qml" line =" 45" />
23
+ <source >English</source >
24
+ <translation ></translation >
25
+ </message >
26
+ <message >
27
+ <location filename =" main.qml" line =" 49" />
28
+ <source >简体中文</source >
29
+ <translation ></translation >
30
+ </message >
31
+ </context >
32
+ </TS >
Original file line number Diff line number Diff line change
1
+ /* !
2
+ *@file main.cpp
3
+ *@brief 程序主文件
4
+ *@version 1.0
5
+ *@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6
+ *@author zhengtianzuo
7
+ */
8
+ #include < QGuiApplication>
9
+ #include < QQmlApplicationEngine>
10
+ #include < QQmlContext>
11
+ #include " QmlLanguage.h"
12
+
13
+ int main (int argc, char *argv[])
14
+ {
15
+ QCoreApplication::setAttribute (Qt::AA_EnableHighDpiScaling);
16
+ QGuiApplication app (argc, argv);
17
+
18
+ QQmlApplicationEngine engine;
19
+ QmlLanguage qmlLanguage (app, engine);
20
+ engine.rootContext ()->setContextProperty (" qmlLanguage" , &qmlLanguage);
21
+ engine.load (QUrl (QLatin1String (" qrc:/main.qml" )));
22
+
23
+ return app.exec ();
24
+ }
Original file line number Diff line number Diff line change
1
+ /*!
2
+ *@file main.qml
3
+ *@brief 主文件
4
+ *@version 1.0
5
+ *@section LICENSE Copyright (C) 2003-2103 CamelSoft Corporation
6
+ *@author zhengtianzuo
7
+ */
8
+ import QtQuick 2.7
9
+ import QtQuick.Controls 2.0
10
+
11
+ ApplicationWindow {
12
+ visible: true
13
+ width: 400
14
+ height: 300
15
+ title: qsTr (" Dialog" )
16
+
17
+ Column{
18
+ width: parent .width
19
+ anchors .horizontalCenter : parent .horizontalCenter
20
+ anchors .verticalCenter : parent .verticalCenter
21
+ anchors .verticalCenterOffset : 20
22
+
23
+ Text {
24
+ id: textLabel
25
+ height: 24
26
+ width: 120
27
+ anchors .horizontalCenter : parent .horizontalCenter
28
+ horizontalAlignment: Text .AlignHCenter
29
+ text: qsTr (" TextLabel" )
30
+ }
31
+ Button{
32
+ id: pushButton
33
+ height: 24
34
+ width: 120
35
+ anchors .horizontalCenter : parent .horizontalCenter
36
+ text: qsTr (" PushButton" )
37
+ }
38
+
39
+ Column{
40
+ anchors .right : parent .right
41
+ anchors .rightMargin : 20
42
+
43
+ RadioButton{
44
+ id: enChk
45
+ text: qsTr (" English" )
46
+ checked: true
47
+ onClicked: {
48
+ qmlLanguage .setLanguage (0 );
49
+ }
50
+ }
51
+ RadioButton{
52
+ id: cnCHK
53
+ text: qsTr (" 简体中文" )
54
+ onClicked: {
55
+ qmlLanguage .setLanguage (1 );
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }
Original file line number Diff line number Diff line change
1
+ <RCC>
2
+ <qresource prefix="/">
3
+ <file>main.qml</file>
4
+ <file>zh_CN.qm</file>
5
+ <file>en_US.qm</file>
6
+ </qresource>
7
+ </RCC>
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" utf-8" ?>
2
+ <!DOCTYPE TS >
3
+ <TS version =" 2.1" language =" zh_CN" >
4
+ <context >
5
+ <name >main</name >
6
+ <message >
7
+ <location filename =" main.qml" line =" 15" />
8
+ <source >Dialog</source >
9
+ <translation >对话框</translation >
10
+ </message >
11
+ <message >
12
+ <location filename =" main.qml" line =" 29" />
13
+ <source >TextLabel</source >
14
+ <translation >文本</translation >
15
+ </message >
16
+ <message >
17
+ <location filename =" main.qml" line =" 36" />
18
+ <source >PushButton</source >
19
+ <translation >按钮</translation >
20
+ </message >
21
+ <message >
22
+ <location filename =" main.qml" line =" 45" />
23
+ <source >English</source >
24
+ <translation ></translation >
25
+ </message >
26
+ <message >
27
+ <location filename =" main.qml" line =" 49" />
28
+ <source >简体中文</source >
29
+ <translation ></translation >
30
+ </message >
31
+ </context >
32
+ </TS >
Original file line number Diff line number Diff line change @@ -187,3 +187,8 @@ QmlPageNavigation: Qml分页显示
187
187
188
188
![ ] ( https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlPageNavigation/show.gif?raw=true )
189
189
190
+
191
+ QmlLanguage: Qml动态语言切换
192
+
193
+ ![ ] ( https://github.com/zhengtianzuo/QtQuickExamples/blob/master/QmlLanguage/show.gif?raw=true )
194
+
You can’t perform that action at this time.
0 commit comments