Skip to content

Commit

Permalink
[优化国际化和代码可读性]: 更新了项目的国际化实现和代码结构,提高了代码的可维护性和翻译的便捷性。
Browse files Browse the repository at this point in the history
- 在`README.md`中增加了国际化实时翻译的更新命令,并修正了参考链接,确保指向最新的QT实用小技巧。
- `cmake/qt_utils.cmake`中为翻译添加了`-no-obsolete`选项,避免更新过时的翻译项。
- `messbox.cpp`中将翻译调用从`QCoreApplication::translate`更改为类特定的`MessBox::tr`,以提高翻译调用的准确性。
- `mainwindow.cpp`中更新了信号连接的写法,使用`q_ptr->connect`代替`QObject::connect`,以确保信号连接的正确性。
- `hashwidget.cc`中更新了翻译调用,并优化了翻译文本,以确保翻译的准确性和一致性。
- `utils.cpp`和`utilstr.h`中调整了翻译相关的结构和命名空间,以适应代码结构的变更。
- `translations/qt-app_zh_CN.ts`中更新了翻译文件,移除了过时的上下文,并添加了新的上下文以匹配源代码的更改。
  • Loading branch information
RealChuan committed Jul 5, 2024
1 parent a10a272 commit bd1daeb
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 127 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@
3. 在使用[qmake](.github/workflows/qmake.yml)时,需要指定`QMAKE_APPLE_DEVICE_ARCHS=x86_64`或者`QMAKE_APPLE_DEVICE_ARCHS=arm64`

- > 国际化实时翻译,当前更改完翻译设置,需要重启程序才能生效;
1. 懒得改代码了;
2. [具体参考:QT实用小技巧(想到就更新)](https://realchuan.github.io/2021/10/12/QT%E5%AE%9E%E7%94%A8%E5%B0%8F%E6%8A%80%E5%B7%A7%EF%BC%88%E6%83%B3%E5%88%B0%E5%B0%B1%E6%9B%B4%E6%96%B0%EF%BC%89/),核心代码;
1. 更新翻译的命令

```bash
cmake --build build --target Qt-App_lupdate
```

2. 懒得改代码了;
3. [具体参考:QT实用小技巧(想到就更新)](https://realchuan.github.io/2021/10/12/QT%E5%AE%9E%E7%94%A8%E5%B0%8F%E6%8A%80%E5%B7%A7%EF%BC%88%E6%83%B3%E5%88%B0%E5%B0%B1%E6%9B%B4%E6%96%B0%EF%BC%89/),核心代码;

```cpp
void Widget::changeEvent(QEvent *e)
Expand Down
4 changes: 3 additions & 1 deletion cmake/qt_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ function(add_translations OUTPUT_NAME)
translations
INCLUDE_DIRECTORIES
directory
${PROJECT_SOURCE_DIR}/src)
${PROJECT_SOURCE_DIR}/src
LUPDATE_OPTIONS
-no-obsolete)

# 确定翻译文件的输出位置
if(CMAKE_HOST_APPLE)
Expand Down
6 changes: 3 additions & 3 deletions src/gui/messbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class MessBox::MessBoxPrivate
messageLabel->setObjectName("MessageLabel");
messageLabel->setWordWrap(true);
messageLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
closeButton = new QPushButton(QCoreApplication::translate("MessBoxPrivate", "Close"), q_ptr);
closeButton = new QPushButton(MessBox::tr("Close"), q_ptr);
closeButton->setObjectName("BlueButton");
yesButton = new QPushButton(QCoreApplication::translate("MessBoxPrivate", "Yes"), q_ptr);
yesButton = new QPushButton(MessBox::tr("Yes"), q_ptr);
yesButton->setObjectName("BlueButton");
noButton = new QPushButton(QCoreApplication::translate("MessBoxPrivate", "No"), q_ptr);
noButton = new QPushButton(MessBox::tr("No"), q_ptr);
noButton->setObjectName("GrayButton");
}

Expand Down
38 changes: 19 additions & 19 deletions src/plugins/coreplugin/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,26 @@ class MainWindow::MainWindowPrivate
trayIcon->setIcon(QIcon(":/icon/icon/app.png"));
trayIcon->setContextMenu(menu);
trayIcon->show();
QObject::connect(trayIcon,
&QSystemTrayIcon::activated,
q_ptr,
[this](QSystemTrayIcon::ActivationReason reason) {
switch (reason) {
case QSystemTrayIcon::DoubleClick: q_ptr->show(); break;
default: break;
}
});
q_ptr->connect(trayIcon,
&QSystemTrayIcon::activated,
q_ptr,
[this](QSystemTrayIcon::ActivationReason reason) {
switch (reason) {
case QSystemTrayIcon::DoubleClick: q_ptr->show(); break;
default: break;
}
});

qApp->setQuitOnLastWindowClosed(false);

QObject::connect(qApp,
&QApplication::applicationStateChanged,
q_ptr,
[this](Qt::ApplicationState state) {
if (state == Qt::ApplicationActive) {
q_ptr->show();
}
});
q_ptr->connect(qApp,
&QApplication::applicationStateChanged,
q_ptr,
[this](Qt::ApplicationState state) {
if (state == Qt::ApplicationActive) {
q_ptr->show();
}
});
}

void setInitWidget(const QString &text) const
Expand All @@ -119,10 +119,10 @@ class MainWindow::MainWindowPrivate

auto *configWidget = new ConfigWidget(q_ptr);
stackedWidget->addWidget(configWidget);
QObject::connect(settingsButton, &QPushButton::clicked, q_ptr, [=] {
q_ptr->connect(settingsButton, &QPushButton::clicked, q_ptr, [=] {
stackedWidget->setCurrentWidget(configWidget);
});
QObject::connect(pluginButton, &QPushButton::clicked, q_ptr, &MainWindow::onAboutPlugins);
q_ptr->connect(pluginButton, &QPushButton::clicked, q_ptr, &MainWindow::onAboutPlugins);

toolsButton->setProperty("Type", Core::CoreWidget::Type::Main);
helpButton->setProperty("Type", Core::CoreWidget::Type::Help);
Expand Down
26 changes: 8 additions & 18 deletions src/plugins/hashplugin/hashwidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ class HashWidget::HashWidgetPrivate
}
}

selectFileButton = new QPushButton(QCoreApplication::translate("HashWidgetPrivate",
"Select File"),
q_ptr);
selectFileButton->setToolTip(
QCoreApplication::translate("HashWidgetPrivate", "Select file to calculate hash."));
selectFileButton = new QPushButton(HashWidget::tr("Select File"), q_ptr);
selectFileButton->setToolTip(HashWidget::tr("Select file to calculate hash."));
selectFileButton->setObjectName("BlueButton");
calculateButton = new QPushButton(QCoreApplication::translate("HashWidgetPrivate",
"Calculate"),
q_ptr);
calculateButton->setToolTip(
QCoreApplication::translate("HashWidgetPrivate", "Calculate hash."));
calculateButton = new QPushButton(HashWidget::tr("Calculate"), q_ptr);
calculateButton->setToolTip(HashWidget::tr("Calculate hash."));
calculateButton->setObjectName("BlueButton");
inputEdit = new QTextEdit(q_ptr);
outputEdit = new QTextEdit(q_ptr);
Expand All @@ -53,21 +47,17 @@ class HashWidget::HashWidgetPrivate
buttonLayout->addWidget(calculateButton);

auto *descriptionLabel = new QLabel(
QCoreApplication::translate(
"HashWidgetPrivate",
"If Input String is file path and file exists, calculate hash of file. "
"Otherwise calculate hash of Input String."),
HashWidget::tr("If Input String is file path and file exists, calculate hash of file. "
"Otherwise calculate hash of Input String."),
q_ptr);
descriptionLabel->setWordWrap(true);

auto *layout = new QVBoxLayout(q_ptr);
layout->addWidget(descriptionLabel);
layout->addWidget(
new QLabel(QCoreApplication::translate("HashWidgetPrivate", "Input:"), q_ptr));
layout->addWidget(new QLabel(HashWidget::tr("Input:"), q_ptr));
layout->addWidget(inputEdit);
layout->addLayout(buttonLayout);
layout->addWidget(
new QLabel(QCoreApplication::translate("HashWidgetPrivate", "Output:"), q_ptr));
layout->addWidget(new QLabel(HashWidget::tr("Output:"), q_ptr));
layout->addWidget(outputEdit);
}

Expand Down
7 changes: 4 additions & 3 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "utils.h"
#include "hostosinfo.h"
#include "utilstr.h"

#include <QTextCodec>
#include <QtWidgets>
Expand Down Expand Up @@ -173,7 +174,7 @@ auto Utils::removeDirectory(const QString &path) -> bool

QDir dir(path);
if (!dir.exists()) {
qWarning() << QCoreApplication::translate("Utils", "Directory does not exist: %1").arg(path);
qWarning() << Tr::tr("Directory does not exist: %1").arg(path);
return false;
}
return dir.removeRecursively();
Expand All @@ -196,7 +197,7 @@ auto Utils::jsonFromFile(const QString &filePath) -> QJsonObject
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << QCoreApplication::translate("Utils", "Cannot open the file: %1").arg(filePath);
qWarning() << Tr::tr("Cannot open the file: %1").arg(filePath);
return {};
}
const QByteArray buf(file.readAll());
Expand All @@ -209,7 +210,7 @@ auto Utils::jsonFromBytes(const QByteArray &bytes) -> QJsonObject
QJsonParseError jsonParseError;
auto jsonDocument = QJsonDocument::fromJson(bytes, &jsonParseError);
if (QJsonParseError::NoError != jsonParseError.error) {
qWarning() << QCoreApplication::translate("Utils", "%1\nOffset: %2")
qWarning() << Tr::tr("%1\nOffset: %2")
.arg(jsonParseError.errorString(), jsonParseError.offset);
return {};
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/utilstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Utils {

struct Tr
{
Q_DECLARE_TR_FUNCTIONS(QtC::Utils)
Q_DECLARE_TR_FUNCTIONS(Utils)
};

} // Utils
} // namespace Utils
127 changes: 48 additions & 79 deletions translations/qt-app_zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
<context>
<name>ConfigWidget</name>
<message>
<location filename="../src/plugins/coreplugin/configwidget.cpp" line="15"/>
<location filename="../src/plugins/coreplugin/configwidget.cpp" line="71"/>
<source>Chinese</source>
<location filename="../src/plugins/coreplugin/configwidget.cpp" line="23"/>
<source>Language(Requires Restart): </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/coreplugin/configwidget.cpp" line="16"/>
<location filename="../src/plugins/coreplugin/configwidget.cpp" line="72"/>
<source>English</source>
<location filename="../src/plugins/coreplugin/configwidget.cpp" line="73"/>
<source>Chinese</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/coreplugin/configwidget.cpp" line="22"/>
<source>Language(Requires Restart): </source>
<location filename="../src/plugins/coreplugin/configwidget.cpp" line="74"/>
<source>English</source>
<translation type="unfinished"></translation>
</message>
</context>
Expand Down Expand Up @@ -92,45 +90,7 @@ Contact Me - Email:
</message>
</context>
<context>
<name>HashWidgetPrivate</name>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="29"/>
<source>Select File</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="33"/>
<source>Select file to calculate hash.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="35"/>
<source>Calculate</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="39"/>
<source>Calculate hash.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="56"/>
<source>If Input String is file path and file exists, calculate hash of file. Otherwise calculate hash of Input String.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="66"/>
<source>Input:</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="70"/>
<source>Output:</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>MessBoxPrivate</name>
<name>GUI::MessBox</name>
<message>
<location filename="../src/gui/messbox.cpp" line="20"/>
<source>Close</source>
Expand Down Expand Up @@ -272,28 +232,55 @@ Contact Me - Email:
<context>
<name>Plugin::HashWidget</name>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="99"/>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="29"/>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="89"/>
<source>Select File</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="107"/>
<source>Input is empty!</source>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="30"/>
<source>Select file to calculate hash.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="114"/>
<source>Hash thread is running!</source>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="32"/>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="115"/>
<source>Calculate</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="118"/>
<source>Calculating...</source>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="33"/>
<source>Calculate hash.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="125"/>
<source>Calculate</source>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="50"/>
<source>If Input String is file path and file exists, calculate hash of file. Otherwise calculate hash of Input String.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="57"/>
<source>Input:</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="60"/>
<source>Output:</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="97"/>
<source>Input is empty!</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="104"/>
<source>Hash thread is running!</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/plugins/hashplugin/hashwidget.cc" line="108"/>
<source>Calculating...</source>
<translation type="unfinished"></translation>
</message>
</context>
Expand Down Expand Up @@ -923,7 +910,7 @@ will also disable the following plugins:
</message>
</context>
<context>
<name>QtC::Utils</name>
<name>Utils</name>
<message>
<location filename="../src/utils/hostosinfo.cpp" line="85"/>
<source>Cannot create OpenGL context.</source>
Expand All @@ -934,36 +921,18 @@ will also disable the following plugins:
<source>Elapsed time: %1.</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Utils</name>
<message>
<location filename="../src/utils/utils.cpp" line="22"/>
<source>Loading QSS file: %1.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/utils/utils.cpp" line="25"/>
<source>Cannot open the file: %1!</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/utils/utils.cpp" line="194"/>
<source>Cannot remove file &quot;%1&quot;: %2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/utils/utils.cpp" line="237"/>
<source>Cannot remove directory &quot;%1&quot;: %2</source>
<location filename="../src/utils/utils.cpp" line="177"/>
<source>Directory does not exist: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/utils/utils.cpp" line="261"/>
<location filename="../src/utils/utils.cpp" line="200"/>
<source>Cannot open the file: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/utils/utils.cpp" line="274"/>
<location filename="../src/utils/utils.cpp" line="213"/>
<source>%1
Offset: %2</source>
<translation type="unfinished"></translation>
Expand Down

0 comments on commit bd1daeb

Please sign in to comment.