Skip to content

Commit

Permalink
feat: add translation of dde-shell plugin
Browse files Browse the repository at this point in the history
Add translation of dde-shell plugin

Log: add translation of dde-shell plugin
Influence: translation
  • Loading branch information
yixinshark committed Apr 9, 2024
1 parent 536badf commit 62f54d9
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ set(DDE_SHELL_PLUGIN_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/dde-shell CACHE STRING
set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}/dde-shell" CACHE STRING "Headers install path")
set(CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/DDEShell" CACHE STRING "CMake config file install directory")
set(QML_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/qt${QT_VERSION_MAJOR}/qml" CACHE STRING "Qml plugin install directory")
set(DDE_SHELL_TRANSLATION_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/dde-shell" CACHE STRING "Translation install directory")
# avoid warning when `D_APPLET_CLASS` used in .cpp
set(CMAKE_AUTOMOC_MACRO_NAMES "D_APPLET_CLASS")

find_package(ECM REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH};${ECM_MODULE_PATH};${PROJECT_SOURCE_DIR}/cmake")
include(DDEShellPackageMacros)

find_package(Qt${QT_VERSION_MAJOR} ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Core Gui Concurrent Quick WaylandClient DBus)
find_package(Qt${QT_VERSION_MAJOR} ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Core Gui Concurrent Quick WaylandClient DBus LinguistTools)
find_package(Dtk${DTK_VERSION_MAJOR} REQUIRED COMPONENTS Core Gui)
find_package(WaylandProtocols REQUIRED)
find_package(PkgConfig REQUIRED)
Expand Down
50 changes: 50 additions & 0 deletions cmake/DDEShellPackageMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,53 @@ function(ds_install_package)
install(TARGETS ${_config_TARGET} DESTINATION ${DDE_SHELL_PLUGIN_INSTALL_DIR}/)
endif()
endfunction()

function(ds_handle_package_translation)
set(oneValueArgs PACKAGE)
set(multiValueArgs QML_FILES SOURCE_FILES )
cmake_parse_arguments(_config "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if(NOT _config_QML_FILES)
file(GLOB_RECURSE _config_QML_FILES ${CMAKE_CURRENT_SOURCE_DIR}/package/*.qml)
file(GLOB _config_QML_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.qml)
endif()

if(NOT _config_SOURCE_FILES)
file(GLOB _config_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
endif()

set(TRANSLATION_FILES
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_az.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_bo.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_ca.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_es.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_fi.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_fr.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_hu.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_it.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_ja.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_ko.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_nb_NO.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_pl.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_pt_BR.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_ru.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_uk.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_zh_CN.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_zh_HK.ts
${CMAKE_CURRENT_SOURCE_DIR}/translations/${_config_PACKAGE}_zh_TW.ts
)

add_custom_target(${_config_PACKAGE}_translation ALL
SOURCES ${TRANSLATION_FILES}
)

qt_add_translations(${_config_PACKAGE}_translation
TS_FILES ${TRANSLATION_FILES}
SOURCES ${_config_QML_FILES} ${_config_SOURCE_FILES}
QM_FILES_OUTPUT_VARIABLE TRANSLATED_FILES
)

# /usr/share/dde-shell/org.deepin.xxx/translations/org.deepin.xxx.qm
install(FILES ${TRANSLATED_FILES} DESTINATION ${DDE_SHELL_TRANSLATION_INSTALL_DIR}/${_config_PACKAGE}/translations)
endfunction()
3 changes: 2 additions & 1 deletion debian/dde-shell.install
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ usr/lib/*/libdde-dockplugin-interface.so.*
usr/share/dsg/configs/org.deepin.ds.dock/
usr/lib/dde-dock/tmp/plugins
usr/lib/dde-dock/tmp/plugins/quick-trays
usr/share/dde-dock/tmp/translations
usr/share/dde-dock/tmp/translations
usr/share/dde-shell/*/translations
1 change: 1 addition & 0 deletions misc/DDEShellConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ find_package(Qt@QT_VERSION_MAJOR@ COMPONENTS Qml Quick REQUIRED)
include(${CMAKE_CURRENT_LIST_DIR}/DDEShellTargets.cmake)
set(DDE_SHELL_PACKAGE_INSTALL_DIR @CMAKE_INSTALL_PREFIX@/@DDE_SHELL_PACKAGE_INSTALL_DIR@)
set(DDE_SHELL_PLUGIN_INSTALL_DIR @CMAKE_INSTALL_PREFIX@/@DDE_SHELL_PLUGIN_INSTALL_DIR@)
set(DDE_SHELL_TRANSLATION_INSTALL_DIR @CMAKE_INSTALL_PREFIX@/@DDE_SHELL_TRANSLATION_INSTALL_DIR@)
check_required_components(Dtk@DTK_VERSION_MAJOR@Core)

include("${CMAKE_CURRENT_LIST_DIR}/DDEShellPackageMacros.cmake")
1 change: 1 addition & 0 deletions panels/dock/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ if (BUILD_WITH_X11)
endif(BUILD_WITH_X11)

ds_install_package(PACKAGE org.deepin.ds.dock TARGET dockpanel)
ds_handle_package_translation(PACKAGE org.deepin.ds.dock)

# sub plugins
add_subdirectory(clipboarditem)
Expand Down
1 change: 1 addition & 0 deletions panels/dock/clipboarditem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ target_link_libraries(dock-clipboarditem PRIVATE
)

ds_install_package(PACKAGE org.deepin.ds.dock.clipboarditem TARGET dock-clipboarditem)
ds_handle_package_translation(PACKAGE org.deepin.ds.dock.clipboarditem)
1 change: 1 addition & 0 deletions panels/dock/searchitem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ target_link_libraries(dock-searchitem PRIVATE
)

ds_install_package(PACKAGE org.deepin.ds.dock.searchitem TARGET dock-searchitem)
ds_handle_package_translation(PACKAGE org.deepin.ds.dock.searchitem)
1 change: 1 addition & 0 deletions panels/dock/taskmanager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ endif(BUILD_WITH_X11)

ds_install_package(PACKAGE org.deepin.ds.dock.taskmanager TARGET dock-taskmanager)
dtk_add_config_meta_files(APPID org.deepin.ds.dock FILES dconfig/org.deepin.ds.dock.taskmanager.json)
ds_handle_package_translation(PACKAGE org.deepin.ds.dock.taskmanager)
1 change: 1 addition & 0 deletions panels/notification/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ target_link_libraries(notificationpanel PRIVATE
)

ds_install_package(PACKAGE org.deepin.ds.notification TARGET notificationpanel)
ds_handle_package_translation(PACKAGE org.deepin.ds.notification)
1 change: 1 addition & 0 deletions panels/osd/default/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ target_link_libraries(osd-default PRIVATE
)

ds_install_package(PACKAGE org.deepin.ds.osd.default)
ds_handle_package_translation(PACKAGE org.deepin.ds.osd.default)
1 change: 1 addition & 0 deletions panels/osd/displaymode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ target_link_libraries(osd-displaymode PRIVATE
)

ds_install_package(PACKAGE org.deepin.ds.osd.displaymode TARGET osd-displaymode)
ds_handle_package_translation(PACKAGE org.deepin.ds.osd.displaymode)
56 changes: 53 additions & 3 deletions shell/appletloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include <QMap>
#include <QLoggingCategory>
#include <QElapsedTimer>
#include <QTranslator>
#include <QApplication>
#include <QFile>
#include <DWindowManagerHelper>

DS_BEGIN_NAMESPACE
Expand Down Expand Up @@ -65,6 +68,13 @@ class DAppletLoaderPrivate : public DObjectPrivate
return groups;
}

~DAppletLoaderPrivate()
{
for (const auto &tl : std::as_const(m_pluginTranslators)) {
tl->deleteLater();
}
}

bool doLoad(DApplet *applet);
void doCreateRootObject(DApplet *applet);
bool doInit(DApplet *applet);
Expand All @@ -75,7 +85,11 @@ class DAppletLoaderPrivate : public DObjectPrivate

void createChildren(DApplet *applet);

void loadTranslation(const DPluginMetaData &pluginData);
void removeTranslation(const QString &pluginId);

QPointer<DApplet> m_applet = nullptr;
QMap<QString, QTranslator *> m_pluginTranslators;

D_DECLARE_PUBLIC(DAppletLoader);
};
Expand All @@ -86,6 +100,9 @@ DAppletLoader::DAppletLoader(class DApplet *applet, QObject *parent)
{
D_D(DAppletLoader);
d->m_applet = applet;
connect(this, &DAppletLoader::failed, this, [d](const QString &pluginId) {
d->removeTranslation(pluginId);
});
}

DAppletLoader::~DAppletLoader()
Expand All @@ -96,6 +113,7 @@ DAppletLoader::~DAppletLoader()
void DAppletLoader::exec()
{
D_D(DAppletLoader);
d->loadTranslation(d->m_applet->pluginMetaData());

if (!d->load(d->m_applet))
return;
Expand Down Expand Up @@ -125,7 +143,7 @@ void DAppletLoaderPrivate::doCreateRootObject(DApplet *applet)
if (!rootObject) {
D_Q(DAppletLoader);
qCWarning(dsLoaderLog) << "Create root failed:" << applet->pluginId();
Q_EMIT q->failed();
Q_EMIT q->failed(applet->pluginId());
}
});

Expand Down Expand Up @@ -155,7 +173,7 @@ bool DAppletLoaderPrivate::doLoad(DApplet *applet)
if (auto containment = qobject_cast<DContainment *>(applet->parentApplet())) {
containment->removeApplet(applet);
}
Q_EMIT q->failed();
Q_EMIT q->failed(applet->pluginId());
return false;
}
return true;
Expand All @@ -171,7 +189,7 @@ bool DAppletLoaderPrivate::doInit(DApplet *applet)
if (auto containment = qobject_cast<DContainment *>(applet->parentApplet())) {
containment->removeApplet(applet);
}
Q_EMIT q->failed();
Q_EMIT q->failed(applet->pluginId());
return false;
}
return true;
Expand Down Expand Up @@ -236,4 +254,36 @@ bool DAppletLoaderPrivate::init(DApplet *applet)
return true;
}

void DAppletLoaderPrivate::loadTranslation(const DPluginMetaData &pluginData)
{
const QString baseDir = pluginData.pluginDir();
const QString pluginId = pluginData.pluginId();

QString pluginTransFilePath = QString(baseDir + "/translations/" + pluginId + "_%1.qm").arg(QLocale::system().name());
if (QFile::exists(pluginTransFilePath)) {
auto translator = new QTranslator(qApp);
if (translator->load(pluginTransFilePath)) {
m_pluginTranslators[pluginId] = translator;
qApp->installTranslator(translator);
} else {
qCWarning(dsLoaderLog) << "Failed to load translation:" << pluginTransFilePath;
translator->deleteLater();
}
}

const auto children = DPluginLoader::instance()->childrenPlugin(pluginId);
for (const auto &childPluginData : children) {
loadTranslation(childPluginData);
}
}

void DAppletLoaderPrivate::removeTranslation(const QString &pluginId)
{
if (m_pluginTranslators.contains(pluginId)) {
qApp->removeTranslator(m_pluginTranslators.value(pluginId));
m_pluginTranslators.value(pluginId)->deleteLater();
m_pluginTranslators.remove(pluginId);
}
}

DS_END_NAMESPACE
2 changes: 1 addition & 1 deletion shell/appletloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DAppletLoader : public QObject, public DTK_CORE_NAMESPACE::DObject
DApplet *applet() const;

Q_SIGNALS:
void failed();
void failed(const QString &pluginId);
};

DS_END_NAMESPACE
8 changes: 5 additions & 3 deletions shell/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ class AppletManager
auto loader = new DAppletLoader(applet);
m_loaders << loader;

QObject::connect(loader, &DAppletLoader::failed, qApp, [this, loader]() {
m_loaders.removeOne(loader);
loader->deleteLater();
QObject::connect(loader, &DAppletLoader::failed, qApp, [this, loader, pluginIds](const QString &pluginId) {
if (pluginIds.contains(pluginId)) {
m_loaders.removeOne(loader);
loader->deleteLater();
}
});
}
}
Expand Down

0 comments on commit 62f54d9

Please sign in to comment.