Skip to content

Commit

Permalink
feat: Add chinese translation for tools
Browse files Browse the repository at this point in the history
  Add translation for dde-dconfig and dde-dconfig-editor.
  Select system language as default description language.

Issue: linuxdeepin/dtk#66
  • Loading branch information
18202781743 committed Sep 13, 2023
1 parent d9da95b commit 0a59816
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ License: CC-BY-4.0
Files: *.md
Copyright: None
License: CC0-1.0

# ts .tx/files
Files: *.ts .tx/*
Copyright: None
License: CC0-1.0
24 changes: 24 additions & 0 deletions dconfig-center/common/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <QDBusArgument>
#include <QJsonDocument>
#include <DConfigFile>
#include <dstandardpaths.h>
#include <QCoreApplication>
#include <QTranslator>

using ResourceId = QString;
using AppId = QString;
Expand Down Expand Up @@ -194,3 +197,24 @@ static QVariant stringToQVariant(const QString &s)
return doc.toVariant();
return s;
}

static QStringList translationDirs()
{
QStringList result;
result << QCoreApplication::applicationDirPath();
for (auto item : Dtk::Core::DStandardPaths::standardLocations(QStandardPaths::DataLocation)) {
result << item + "/translations";
};
return result;
}

static void loadTranslation(const QString &fileName)
{
auto translator = new QTranslator(QCoreApplication::instance());
for (auto item :translationDirs()) {
if (translator->load(QLocale::system(), fileName, "_", item, ".qm")) {
QCoreApplication::installTranslator(translator);
break;
}
}
}
13 changes: 11 additions & 2 deletions dconfig-center/dde-dconfig-editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
endif ()

set(REQUIRED_QT_VERSION 5.11.3)
find_package(Qt${QT_VERSION_MAJOR} ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Core Gui Widgets DBus)
find_package(Qt${QT_VERSION_MAJOR} ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Core Gui Widgets DBus LinguistTools)
find_package(Dtk${DTK_VERSION_MAJOR}Core REQUIRED)
find_package(Dtk${DTK_VERSION_MAJOR}Gui REQUIRED)
find_package(Dtk${DTK_VERSION_MAJOR}Widget REQUIRED)
Expand Down Expand Up @@ -55,9 +55,17 @@ set(SOURCES
oemdialog.cpp
)

set(TRANSLATIONS translates/dde-dconfig-editor_zh_CN.ts)
if(EnableDtk6)
qt6_add_translation(QM_FILES ${TRANSLATIONS})
endif()
if (EnableDtk5)
qt5_add_translation(QM_FILES ${TRANSLATIONS})
endif()

set(QRC dde-dconfig-editor.qrc)

ADD_EXECUTABLE(dde-dconfig-editor ${HEADERS} ${SOURCES} ${QRC} ${DCONFIG_DBUS_XML})
ADD_EXECUTABLE(dde-dconfig-editor ${HEADERS} ${SOURCES} ${QRC} ${DCONFIG_DBUS_XML} ${QM_FILES})

set(COMMON_LIBS
Qt${QT_VERSION_MAJOR}::Core
Expand All @@ -77,3 +85,4 @@ target_include_directories(${PROJECT_NAME} PUBLIC
)

install(TARGETS dde-dconfig-editor DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_DATADIR}/dde-dconfig-editor/translations/)
12 changes: 6 additions & 6 deletions dconfig-center/dde-dconfig-editor/exportdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ ExportDialog::ExportDialog(QWidget *parent)
m_exportView->setEditTriggers(QAbstractItemView::NoEditTriggers);

QCheckBox *checkbox = new QCheckBox();
checkbox->setText("全选");
m_exportBtn = new DSuggestButton("导出");
checkbox->setText(tr("select all"));
m_exportBtn = new DSuggestButton(tr("export"));
m_exportBtn->setEnabled(false);
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addStretch(1);
Expand All @@ -46,7 +46,7 @@ ExportDialog::ExportDialog(QWidget *parent)
void ExportDialog::loadData(const QString &language)
{
auto model = new QStandardItemModel(this);
model->setHorizontalHeaderLabels(QStringList() << QStringLiteral("内容"));
model->setHorizontalHeaderLabels(QStringList() << tr("content"));
connect(model, &QStandardItemModel::itemChanged, this, &ExportDialog::treeItemChanged);
connect(model, &QStandardItemModel::itemChanged, this, [this] {
m_exportBtn->setEnabled(hasChildItemChecked());
Expand Down Expand Up @@ -160,7 +160,7 @@ bool ExportDialog::hasChildItemChecked()

void ExportDialog::saveFile()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("导出当前配置"), "", tr("文件(*.csv)"));
QString fileName = QFileDialog::getSaveFileName(this, tr("export current configuration"), "", tr("file(*.csv)"));
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream(&file);
Expand All @@ -175,8 +175,8 @@ void ExportDialog::saveFile()
this->close();
} else {
qWarning() << "stream.status:" << stream.status();
DDialog dialog("文件保存失败!", "",this);
dialog.addButton("确定", true, ButtonWarning);
DDialog dialog(tr("save failed"), "",this);
dialog.addButton(tr("ok"), true, ButtonWarning);
dialog.exec();
}
}
Expand Down
3 changes: 3 additions & 0 deletions dconfig-center/dde-dconfig-editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "mainwindow.h"
#include "helper.hpp"

#include <DApplication>
#include <DWidgetUtil>
Expand All @@ -16,6 +17,8 @@ int main(int argc, char *argv[])
a.setApplicationVersion(VERSION);
a.setApplicationName("dde-dconfig-editor");
a.setOrganizationName("deepin");
a.loadTranslator();
loadTranslation("dde-dconfig-editor");
a.setProductIcon(QIcon::fromTheme(APP_ICON));
a.setWindowIcon(QIcon::fromTheme(APP_ICON));
MainWindow window;
Expand Down
56 changes: 32 additions & 24 deletions dconfig-center/dde-dconfig-editor/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ MainWindow::MainWindow(QWidget *parent) :
appListView->setEditTriggers(QAbstractItemView::NoEditTriggers);

auto appHeader = new DSearchEdit();
appHeader->setPlaceHolder("appid");
appHeader->setPlaceholderText("input filter appid");
appHeader->setPlaceHolder(tr("appid"));
appHeader->setPlaceholderText(tr("input filter appid"));
QObject::connect(appHeader, &DSearchEdit::textChanged, [this](const QString &appid){
refreshApps(appid);
});
Expand All @@ -71,8 +71,8 @@ MainWindow::MainWindow(QWidget *parent) :
resourceListView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
auto resourceHeader = new DSearchEdit();
resourceHeader->setMinimumWidth(200);
resourceHeader->setPlaceHolder("resource");
resourceHeader->setPlaceholderText("input filter resource");
resourceHeader->setPlaceHolder(tr("resource"));
resourceHeader->setPlaceholderText(tr("input filter resource"));
QObject::connect(resourceHeader, &DSearchEdit::textChanged, [this](const QString &resourceid){
const auto &appid = appListView->model()->data(appListView->currentIndex(), ConfigUserRole + 2).toString();
refreshAppResources(appid, resourceid);
Expand All @@ -89,8 +89,8 @@ MainWindow::MainWindow(QWidget *parent) :
contentView->setObjectName(QStringLiteral("contentView"));

auto contentHeader = new DSearchEdit();
contentHeader->setPlaceHolder("keys");
contentHeader->setPlaceholderText("input filter keys");
contentHeader->setPlaceHolder(tr("keys"));
contentHeader->setPlaceholderText(tr("input filter keys"));
QObject::connect(contentHeader, &DSearchEdit::textChanged, [this](const QString &keyid){
const auto &appid = appListView->model()->data(appListView->currentIndex(), ConfigUserRole + 2).toString();
const auto &resourceId = resourceListView->model()->data(resourceListView->currentIndex(), ConfigUserRole + 3).toString();
Expand Down Expand Up @@ -153,7 +153,7 @@ MainWindow::MainWindow(QWidget *parent) :
// set history
DTitlebar *titlebar = this->titlebar();
titlebar->setIcon(QIcon(APP_ICON));
connect(titlebar->menu()->addAction("setting history"), &QAction::triggered, [this](){
connect(titlebar->menu()->addAction(tr("setting history")), &QAction::triggered, [this](){
qInfo() << "show history view";
historyView->show();

Expand All @@ -167,14 +167,14 @@ MainWindow::MainWindow(QWidget *parent) :
refreshResourceKeys(appid, resourceId, subpath, contentHeader->text());
});

connect(titlebar->menu()->addAction("export"), &QAction::triggered, [this]() {
connect(titlebar->menu()->addAction(tr("export")), &QAction::triggered, [this]() {
qInfo() << "export setting";
exportView->loadData(contentView->language());
exportView->show();
});
exportView = new ExportDialog(this);
exportView->setFixedSize(QSize(400, 600));
connect(titlebar->menu()->addAction("OEM"), &QAction::triggered, [this]() {
connect(titlebar->menu()->addAction(tr("OEM")), &QAction::triggered, [this]() {
oemView->loadData(contentView->language());
oemView->show();
});
Expand Down Expand Up @@ -289,32 +289,40 @@ void MainWindow::refreshResourceKeys(const QString &appid, const QString &resour
void MainWindow::installTranslate()
{
DTitlebar *titlebar = this->titlebar();
auto languageMenu = titlebar->menu()->addMenu("config language");
auto defaultAction = languageMenu->addAction("default");
auto languageMenu = titlebar->menu()->addMenu(tr("config language"));
auto defaultAction = languageMenu->addAction(tr("default"));
defaultAction->setCheckable(true);
auto chineseAction = languageMenu->addAction("chinese");
auto chineseAction = languageMenu->addAction(tr("chinese"));
chineseAction->setCheckable(true);
auto englishAction = languageMenu->addAction("english");
auto englishAction = languageMenu->addAction(tr("english"));
englishAction->setCheckable(true);
QActionGroup *languageGroup = new QActionGroup(this);
languageGroup->addAction(defaultAction);
languageGroup->addAction(chineseAction);
languageGroup->addAction(englishAction);
defaultAction->setChecked(true);

connect(defaultAction, &QAction::triggered, [this](){
connect(defaultAction, &QAction::toggled, [this](){
contentView->setLanguage("");
});
connect(chineseAction, &QAction::triggered, [this](){
connect(chineseAction, &QAction::toggled, [this](){
contentView->setLanguage("zh_CN");
});
connect(englishAction, &QAction::triggered, [this](){
connect(englishAction, &QAction::toggled, [this](){
contentView->setLanguage("en_US");
});
connect(contentView, &Content::languageChanged, this, [this](){
emit resourceListView->clicked(resourceListView->currentIndex());
});

const auto systemLanguage = QLocale::system().name();
qDebug() << systemLanguage;
if (systemLanguage == "zh_CN") {
chineseAction->setChecked(true);
} else if (systemLanguage == "en_US") {
englishAction->setChecked(true);
} else {
defaultAction->setChecked(true);
}
}

void MainWindow::translateAppName()
Expand Down Expand Up @@ -540,10 +548,10 @@ void Content::onCustomContextMenuRequested(QWidget *widget, const QString &appid

QMenu *menu = new QMenu(widget);

QAction *exportAction = menu->addAction("导出");
QAction *copyAction = menu->addAction("复制");
QAction *copyCmdAction = menu->addAction("复制命令");
QAction *resetCmdAction = menu->addAction("重置");
QAction *exportAction = menu->addAction(tr("export"));
QAction *copyAction = menu->addAction(tr("copy value"));
QAction *copyCmdAction = menu->addAction(tr("convert to cmd"));
QAction *resetCmdAction = menu->addAction(tr("reset value"));

QString setCmd = QString("dde-dconfig set %1 -r %2 %3 -v %4").arg(appid).arg(resource).arg(key).arg(value);
QString getCmd = QString("dde-dconfig get %1 -r %2 %3").arg(appid).arg(resource).arg(key);
Expand All @@ -552,7 +560,7 @@ void Content::onCustomContextMenuRequested(QWidget *widget, const QString &appid
getCmd.append(QString(" -s %1").arg(subpath));
}
connect(exportAction, &QAction::triggered, this, [this, appid, resource, subpath, key, value, description, setCmd, getCmd]{
QString fileName = QFileDialog::getSaveFileName(this, tr("导出当前配置"), "", tr("文件(*.csv)"));
QString fileName = QFileDialog::getSaveFileName(this, tr("export current configuration"), "", tr("file(*.csv)"));
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream(&file);
Expand All @@ -566,8 +574,8 @@ void Content::onCustomContextMenuRequested(QWidget *widget, const QString &appid
file.close();
if (stream.status() != QTextStream::Ok) {
qWarning() << "stream.status:" << stream.status();
DDialog dialog("文件保存失败!", "",this);
dialog.addButton("确定", true, DDialog::ButtonWarning);
DDialog dialog("save failed", "",this);
dialog.addButton("ok", true, DDialog::ButtonWarning);
dialog.exec();
}
}
Expand Down
20 changes: 10 additions & 10 deletions dconfig-center/dde-dconfig-editor/oemdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ OEMDialog::OEMDialog(QWidget *parent)
m_exportView->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_exportView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);

m_exportBtn = new DSuggestButton("OEM");
m_exportBtn = new DSuggestButton(tr("OEM"));
m_exportBtn->setEnabled(false);
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addStretch(1);
Expand Down Expand Up @@ -176,7 +176,7 @@ void OEMDialog::treeItemChanged(DStandardItem *valueItem)

void OEMDialog::saveFiles()
{
QString dirName = QFileDialog::getExistingDirectory(this, tr("导出目录"), QDir::homePath(),
QString dirName = QFileDialog::getExistingDirectory(this, tr("export directory"), QDir::homePath(),
QFileDialog::ShowDirsOnly |
QFileDialog::DontResolveSymlinks);
if (dirName.isEmpty())
Expand Down Expand Up @@ -222,8 +222,8 @@ void OEMDialog::saveCSVFile(const QString &dirName)
this->close();
} else {
qWarning() << "stream.status:" << out.status();
DDialog dialog("文件保存失败!", "",this);
dialog.addButton("确定", true, ButtonWarning);
DDialog dialog(tr("save filed"), "",this);
dialog.addButton(tr("ok"), true, ButtonWarning);
dialog.exec();
}
}
Expand Down Expand Up @@ -257,7 +257,7 @@ void OEMDialog::displayChangedResult()
<< new DStandardItem(path));
}
}
DSuggestButton *btn = new DSuggestButton("确认");
DSuggestButton *btn = new DSuggestButton(tr("ok"));
QHBoxLayout *hlayout = new QHBoxLayout;
hlayout->addStretch(1);
hlayout->addSpacing(20);
Expand All @@ -283,9 +283,9 @@ void OEMDialog::closeEvent(QCloseEvent *event)
event->accept();
return;
}
DDialog dialog("文件未保存,确定退出!", "",this);
dialog.addButton("取消", true, ButtonWarning);
dialog.addButton("确定", true, ButtonWarning);
DDialog dialog(tr("file doesn't save"), "",this);
dialog.addButton(tr("cancle"), true, ButtonWarning);
dialog.addButton(tr("ok"), true, ButtonWarning);
connect(dialog.getButton(0), &QPushButton::clicked, this, [event]{
event->ignore();
});
Expand Down Expand Up @@ -330,8 +330,8 @@ void OEMDialog::createJsonFile(const QString &fileName, const QList<DStandardIte
file.close();
if (out.status() != QTextStream::Ok) {
qWarning() << "stream.status:" << out.status();
DDialog dialog("文件保存失败!", "",this);
dialog.addButton("确定", true, ButtonWarning);
DDialog dialog("save failed", "",this);
dialog.addButton("ok", true, ButtonWarning);
dialog.exec();
}
qDebug() << byteArray;
Expand Down
Loading

0 comments on commit 0a59816

Please sign in to comment.