Skip to content

Commit

Permalink
add qt translation
Browse files Browse the repository at this point in the history
  • Loading branch information
AMOSOMNUM committed Jan 3, 2024
1 parent a1e951b commit af13a9f
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 17 deletions.
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ else()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/release)
endif()

find_package(Qt6 REQUIRED COMPONENTS Quick Svg Network HttpServer Concurrent)
find_package(Qt6 REQUIRED COMPONENTS Quick Svg Network HttpServer Concurrent LinguistTools)

if(${Qt6_VERSION} VERSION_GREATER_EQUAL "6.3")
qt_standard_project_setup()
Expand Down Expand Up @@ -106,6 +106,22 @@ qt_add_qml_module(SAST_Evento_Desktop
SOURCES ${SOURCE_IMPORT_FILES}
)

# 多语言配置
list(APPEND TS_FILES ${CMAKE_SOURCE_DIR}/translations/SAST_Evento_en.ts)
set_source_files_properties(${TS_FILES}
PROPERTIES OUTPUT_LOCATION "${CMAKE_SOURCE_DIR}/translations")
file(GLOB_RECURSE QML_FILES
${CMAKE_SOURCE_DIR}/qml/*.qml
)
qt_add_lupdate(SAST_Evento_Desktop
TS_FILES ${TS_FILES}
SOURCES ${QML_FILES}
)
qt_add_lrelease(SAST_Evento_Desktop
TS_FILES ${TS_FILES}
QM_FILES_OUTPUT_VARIABLE QM_FILES
)

set_property(TARGET ${PROJECT_NAME} PROPERTY
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_SOURCE_DIR}/.cmake/Info.plist.in")

Expand Down
1 change: 1 addition & 0 deletions SAST_Evento.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@
<file>app.icns</file>
<file>qml/page/T_LessonPic.qml</file>
<file>res/image/department/SRD_lesson_pic_bg.jpg</file>
<file>translations/SAST_Evento_en.qm</file>
</qresource>
</RCC>
6 changes: 3 additions & 3 deletions qml/page/T_Settings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ FluScrollablePage {
delegate: FluRadioButton {
checked: settings.langMode === modelData
text: lang_text
clickListener: function () {
settings.langMode = langSetting[index]
clickListener:function () {
settings.langMode = langSetting.model[index]
Lang.changeLangMode(index)
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ FluScrollablePage {
target: CheckUpdate
function onCheckSuccessEvent(version, description) {
btn_progress.progress = 1
if (version === appInfo.version)
if (version === AppInfo.version)
showInfo(lang.lang_is_currently_latest_version, 4000)
}
}
Expand Down
17 changes: 8 additions & 9 deletions qml/window/LoginWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.wangwenx190.FramelessHelper

FluWindow {
id: window
title: lang.lang_login
title: qsTr("登录")
width: 278
height: 378
fixSize: true
Expand Down Expand Up @@ -52,18 +52,17 @@ FluWindow {
target: LoginController
function onLoadPermissionErrorEvent(message) {
hideLoading()
showError(lang.lang_error + message, 4000)
showError(qsTr("登录失败:") + message, 4000)
}
}

Connections {
target: LoginController
function onLoginFailed(reason) {
hideLoading()
system_tray.showMessage(lang.lang_login_failed,
lang.lang_error + reason)
system_tray.showMessage(qsTr("登录失败"), qsTr("错误:") + reason)
window.requestActivate()
showError(lang.lang_login_failed, 4000)
showError(qsTr("登录失败"), 4000)
}
}

Expand Down Expand Up @@ -93,7 +92,7 @@ FluWindow {
tooltip: "SAST Evento"
menu: Menu {
MenuItem {
text: lang.lang_exit
text: qsTr("退出")
onTriggered: {
window.deleteWindow()
FluApp.closeApp()
Expand All @@ -111,7 +110,7 @@ FluWindow {

FluButton {
id: btn_login
text: lang.lang_use_link
text: qsTr(" 使用 Link 登入")
font.pixelSize: 16
font.bold: true
textColor: Qt.rgba(250 / 255, 250 / 255, 250 / 255, 1)
Expand Down Expand Up @@ -146,7 +145,7 @@ FluWindow {

FluIconButton {
id: btn_visitor
text: lang.lang_guest_login
text: qsTr("访客登录")
iconSource: FluentIcons.GuestUser
display: Button.TextBesideIcon
anchors {
Expand All @@ -162,7 +161,7 @@ FluWindow {
}

FluText {
text: lang.lang_by_cpp + appInfo.version
text: qsTr("NJUPT SAST C++组开发\n") + AppInfo.version
color: FluColors.Grey100
font: FluTextStyle.Caption
horizontalAlignment: Text.AlignHCenter
Expand Down
2 changes: 1 addition & 1 deletion qml/window/MainWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ FluWindow {
| FluContentDialogType.PositiveButton
onNegativeClicked: {
window.hide()
system_tray.showMessage(lang.lang_hint, lang.lang_hint_info)
system_tray.showMessage(qsTr("提示"), qsTr("SAST Evento已隐藏至托盘,点击图标可再次激活窗口。"))
}
positiveText: lang.lang_exit
neutralText: lang.lang_cancel
Expand Down
35 changes: 32 additions & 3 deletions src/lang/Lang.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
#ifndef LANG_H
#define LANG_H

#include <QObject>
#include <QLocale>
#include <QtQml>

class Lang : public QObject {
Q_OBJECT
QML_NAMED_ELEMENT(Lang)
QML_SINGLETON

private:
QTranslator* translator = nullptr;
public:
Lang() {
QLocale::setDefault(QLocale::Chinese);
}
~Lang() {
if (translator)
translator->deleteLater();
}
public:
enum LanguageMode {
System,
Expand All @@ -17,7 +26,27 @@ class Lang : public QObject {
};
Q_ENUM(LanguageMode)
public:
Q_INVOKABLE void changeLangMode(LanguageMode) {}
Q_INVOKABLE void changeLangMode(LanguageMode mode) {
QLocale next;
switch (mode) {
case System: next = QLocale::system();break;
case Chinese: next = QLocale::Chinese;break;
case English: next = QLocale::English;break;
}
if (next.language() == QLocale())
return;
switch (next.language()) {
case QLocale::Chinese:QCoreApplication::removeTranslator(translator);break;
case QLocale::English:
if (!translator) {
translator = new QTranslator();
translator->load("SAST_Evento_en.qm", ":/translations");
}
QCoreApplication::installTranslator(translator);
break;
}
QLocale::setDefault(next);
}
};

#endif // LANG_H
95 changes: 95 additions & 0 deletions translations/SAST_Evento_en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_GB" sourcelanguage="zh_CN">
<context>
<name>LoginWindow</name>
<message>
<location filename="../qml/window/LoginWindow.qml" line="11"/>
<source>登录</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/window/LoginWindow.qml" line="55"/>
<source>登录失败:</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/window/LoginWindow.qml" line="63"/>
<location filename="../qml/window/LoginWindow.qml" line="65"/>
<source>登录失败</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/window/LoginWindow.qml" line="63"/>
<source>错误:</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/window/LoginWindow.qml" line="95"/>
<source>退出</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/window/LoginWindow.qml" line="113"/>
<source> 使用 Link 登入</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/window/LoginWindow.qml" line="148"/>
<source>访客登录</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/window/LoginWindow.qml" line="164"/>
<source>NJUPT SAST C++组开发
</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>MainWindow</name>
<message>
<location filename="../qml/window/MainWindow.qml" line="64"/>
<source>提示</source>
<translation>HINT</translation>
</message>
<message>
<location filename="../qml/window/MainWindow.qml" line="64"/>
<source>SAST Evento已隐藏至托盘,点击图标可再次激活窗口。</source>
<translation>SAST Evento is hidden in the system tray. Click the icon to activate the window.</translation>
</message>
</context>
<context>
<name>T_LessonPic</name>
<message>
<location filename="../qml/page/T_LessonPic.qml" line="36"/>
<source>%1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/page/T_LessonPic.qml" line="51"/>
<source>第%1周</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/page/T_LessonPic.qml" line="67"/>
<source>授课课表</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/page/T_LessonPic.qml" line="173"/>
<source>时间:%1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/page/T_LessonPic.qml" line="185"/>
<source>地点:%1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/page/T_LessonPic.qml" line="197"/>
<source>主题:%1</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

0 comments on commit af13a9f

Please sign in to comment.