diff --git a/clients/aurorae/src/kwindecoration.desktop b/clients/aurorae/src/kwindecoration.desktop index 45b36709f6..fde1f43646 100644 --- a/clients/aurorae/src/kwindecoration.desktop +++ b/clients/aurorae/src/kwindecoration.desktop @@ -51,3 +51,6 @@ Comment[zh_TW]=KWin 視窗裝飾 [PropertyDef::X-Plasma-MainScript] Type=QString + +[PropertyDef::X-KWin-Config-TranslationDomain] +Type=QString diff --git a/clients/aurorae/themes/plastik/package/metadata.desktop b/clients/aurorae/themes/plastik/package/metadata.desktop index 9b68d14f9b..5e4d944222 100644 --- a/clients/aurorae/themes/plastik/package/metadata.desktop +++ b/clients/aurorae/themes/plastik/package/metadata.desktop @@ -141,5 +141,6 @@ X-KDE-PluginInfo-Version=1.0 X-KDE-PluginInfo-Depends= X-KDE-PluginInfo-License=GPL X-KDE-ServiceTypes=KWin/Decoration +X-KWin-Config-TranslationDomain=kwin_clients Type=Service diff --git a/kcmkwin/kwindecoration/CMakeLists.txt b/kcmkwin/kwindecoration/CMakeLists.txt index 448817765d..848a6b601b 100644 --- a/kcmkwin/kwindecoration/CMakeLists.txt +++ b/kcmkwin/kwindecoration/CMakeLists.txt @@ -4,6 +4,7 @@ add_definitions(-DTRANSLATION_DOMAIN=\"kcmkwindecoration\") # need a header file from Aurorae sources include_directories( ${KWIN_SOURCE_DIR}/clients/aurorae/src/lib + ../utils/ ) set(AURORAE_SOURCE_DIR ${KWIN_SOURCE_DIR}/clients/aurorae/src/lib/) @@ -15,6 +16,7 @@ set(kcm_kwindecoration_PART_SRCS configdialog.cpp preview.cpp decorationmodel.cpp + ../utils/uitranslator.cpp ${AURORAE_SOURCE_DIR}/auroraetheme.cpp ${AURORAE_SOURCE_DIR}/themeconfig.cpp ) diff --git a/kcmkwin/kwindecoration/decorationmodel.cpp b/kcmkwin/kwindecoration/decorationmodel.cpp index 96a2f10677..cea9d20de3 100644 --- a/kcmkwin/kwindecoration/decorationmodel.cpp +++ b/kcmkwin/kwindecoration/decorationmodel.cpp @@ -119,6 +119,7 @@ void DecorationModel::findDecorations() data.type = DecorationModelData::QmlDecoration; data.auroraeName = service->property("X-KDE-PluginInfo-Name").toString(); QString scriptName = service->property("X-Plasma-MainScript").toString(); + data.configTranslationDomain = service->property(QStringLiteral("X-KWin-Config-TranslationDomain")).toString(); data.qmlPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kwin/decorations/" + data.auroraeName + "/contents/" + scriptName); if (data.qmlPath.isEmpty()) { // not a valid QML theme @@ -251,6 +252,8 @@ QVariant DecorationModel::data(const QModelIndex& index, int role) const return m_decorations[ index.row()].qmlPath; case CloseOnDblClickRole: return m_decorations[index.row()].closeDblClick; + case ConfigTranslationDomain: + return m_decorations[index.row()].configTranslationDomain; default: return QVariant(); } diff --git a/kcmkwin/kwindecoration/decorationmodel.h b/kcmkwin/kwindecoration/decorationmodel.h index b2deff15e8..5c018c5e0e 100644 --- a/kcmkwin/kwindecoration/decorationmodel.h +++ b/kcmkwin/kwindecoration/decorationmodel.h @@ -56,6 +56,7 @@ public: QString license; QString auroraeName; QString qmlPath; + QString configTranslationDomain; KDecorationDefines::BorderSize borderSize; KDecorationDefines::BorderSize buttonSize; /** @@ -88,7 +89,8 @@ public: BorderSizesRole = Qt::UserRole + 12, ButtonSizeRole = Qt::UserRole + 13, QmlMainScriptRole = Qt::UserRole + 14, - CloseOnDblClickRole = Qt::UserRole + 15 + CloseOnDblClickRole = Qt::UserRole + 15, + ConfigTranslationDomain = Qt::UserRole + 16 }; explicit DecorationModel(KSharedConfigPtr config, QObject* parent = nullptr); ~DecorationModel(); diff --git a/kcmkwin/kwindecoration/kwindecoration.cpp b/kcmkwin/kwindecoration/kwindecoration.cpp index 726ccf3ef4..48e8f957ab 100644 --- a/kcmkwin/kwindecoration/kwindecoration.cpp +++ b/kcmkwin/kwindecoration/kwindecoration.cpp @@ -34,6 +34,7 @@ #include "decorationmodel.h" #include "auroraetheme.h" #include "preview.h" +#include "uitranslator.h" // Qt #include #include @@ -79,6 +80,7 @@ KWinDecorationModule::KWinDecorationModule(QWidget* parent, const QVariantList & , m_configLoaded(false) , m_decorationButtons(new DecorationButtons(this)) , m_listView(new QQuickView()) + , m_translator(new KLocalizedTranslator(this)) { qmlRegisterType("org.kde.kwin.aurorae", 0, 1, "AuroraeTheme"); qmlRegisterType("org.kde.kwin.kcmdecoration", 0, 1, "PreviewItem"); @@ -99,6 +101,8 @@ KWinDecorationModule::KWinDecorationModule(QWidget* parent, const QVariantList & i18n("(c) 2001 Karol Szwed")); about->addAuthor(i18n("Karol Szwed"), QString(), "gallium@kde.org"); setAboutData(about); + + QCoreApplication::instance()->installTranslator(m_translator); } @@ -403,15 +407,22 @@ void KWinDecorationModule::slotConfigureDecoration() KConfigGroup configGroup = auroraeConfig->group(packageName); KConfigLoader *skeleton = new KConfigLoader(configGroup, &configFile, dlg); // load the ui file + m_translator->setTranslationDomain(index.data(DecorationModel::ConfigTranslationDomain).toString()); QUiLoader *loader = new QUiLoader(dlg); + loader->setLanguageChangeEnabled(true); QFile uiFile(uiPath); uiFile.open(QFile::ReadOnly); QWidget *customConfigForm = loader->load(&uiFile, form); + m_translator->addContextToMonitor(customConfigForm->objectName()); uiFile.close(); form->layout()->addWidget(customConfigForm); // connect the ui file with the skeleton configManager = new KConfigDialogManager(customConfigForm, skeleton); configManager->updateWidgets(); + + // send a custom event to the translator to retranslate using our translator + QEvent le(QEvent::LanguageChange); + QCoreApplication::sendEvent(customConfigForm, &le); } } if (dlg->exec() == QDialog::Accepted) { diff --git a/kcmkwin/kwindecoration/kwindecoration.h b/kcmkwin/kwindecoration/kwindecoration.h index fb9ac7cf12..60f4a52b60 100644 --- a/kcmkwin/kwindecoration/kwindecoration.h +++ b/kcmkwin/kwindecoration/kwindecoration.h @@ -44,6 +44,7 @@ namespace KWin { class DecorationModel; +class KLocalizedTranslator; class KWinDecorationForm : public QWidget, public Ui::KWinDecorationForm { @@ -133,6 +134,8 @@ private: DecorationButtons *m_decorationButtons; QScopedPointer m_listView; + + KLocalizedTranslator *m_translator; }; } //namespace diff --git a/kcmkwin/utils/uitranslator.cpp b/kcmkwin/utils/uitranslator.cpp new file mode 100644 index 0000000000..d839389c99 --- /dev/null +++ b/kcmkwin/utils/uitranslator.cpp @@ -0,0 +1,58 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*********************************************************************/ +#include "uitranslator.h" +// frameworks +#include +// Qt +#include +#include +#include + +namespace KWin +{ + +KLocalizedTranslator::KLocalizedTranslator(QObject* parent) + : QTranslator(parent) +{ +} + +void KLocalizedTranslator::setTranslationDomain(const QString &translationDomain) +{ + m_translationDomain = translationDomain; +} + +void KLocalizedTranslator::addContextToMonitor(const QString &context) +{ + m_monitoredContexts.insert(context); +} + +QString KLocalizedTranslator::translate(const char *context, const char *sourceText, const char *disambiguation, int n) const +{ + if (m_translationDomain.isEmpty() || !m_monitoredContexts.contains(QString::fromUtf8(context))) { + return QTranslator::translate(context, sourceText, disambiguation, n); + } + if (qstrlen(disambiguation) == 0) { + return ki18nd(m_translationDomain.toUtf8().constData(), sourceText).toString(); + } else { + return ki18ndc(m_translationDomain.toUtf8().constData(), disambiguation, sourceText).toString(); + } +} + +} // namespace KWin diff --git a/kcmkwin/utils/uitranslator.h b/kcmkwin/utils/uitranslator.h new file mode 100644 index 0000000000..20532194df --- /dev/null +++ b/kcmkwin/utils/uitranslator.h @@ -0,0 +1,48 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*********************************************************************/ +#ifndef KWIN_KCMUTILS_UITRANSLATOR_H +#define KWIN_KCMUTILS_UITRANSLATOR_H + +#include +#include + +class QWidget; + +namespace KWin +{ + +class KLocalizedTranslator : public QTranslator +{ + Q_OBJECT +public: + explicit KLocalizedTranslator(QObject *parent = 0); + QString translate(const char *context, const char *sourceText, const char *disambiguation = 0, int n = -1) const override; + + void setTranslationDomain(const QString &translationDomain); + void addContextToMonitor(const QString &context); + +private: + QString m_translationDomain; + QSet m_monitoredContexts; +}; + +} + +#endif