[scripting] Support loading translations for UI loaded configs

Scripts and scripted effects can provide a ui file which gets loaded at
runtime. Obviously such a ui file is not translated. This introduces
quite a hack to load the translated strings.

In the metadata.desktop file the scripted component can provide a key
X-KWin-Config-TranslationDomain. The genericscriptedconfig tries to
locate the metadata.desktop file and reads this entry. If it is present
we extract all string properties of the loaded UI and pass them through
ki18nd.

REVIEW: 118585
This commit is contained in:
Martin Gräßlin 2014-06-06 09:16:51 +02:00
parent d0c83ddbf3
commit 0c4aaaf4ad
5 changed files with 26 additions and 1 deletions

View file

@ -168,3 +168,4 @@ X-Plasma-MainScript=code/main.js
X-KDE-PluginKeyword=kwin4_effect_login
X-KDE-Library=kcm_kwin4_genericscripted
X-KDE-ParentComponents=kwin4_effect_login
X-KWin-Config-TranslationDomain=kwin_effects

View file

@ -171,3 +171,4 @@ X-Plasma-MainScript=code/main.js
X-KDE-PluginKeyword=kwin4_effect_translucency
X-KDE-Library=kcm_kwin4_genericscripted
X-KDE-ParentComponents=kwin4_effect_translucency
X-KWin-Config-TranslationDomain=kwin_effects

View file

@ -1,4 +1,5 @@
set(kcm_kwin4_genericscripted_SRCS genericscriptedconfig.cpp)
include_directories(${KWIN_SOURCE_DIR}/kcmkwin/utils/)
set(kcm_kwin4_genericscripted_SRCS genericscriptedconfig.cpp ${KWIN_SOURCE_DIR}/kcmkwin/utils/uitranslator.cpp)
qt5_add_dbus_interface(kcm_kwin4_genericscripted_SRCS ${kwin_effects_dbus_xml} kwineffects_interface)
add_library(kcm_kwin4_genericscripted MODULE ${kcm_kwin4_genericscripted_SRCS})
target_link_libraries( kcm_kwin4_genericscripted

View file

@ -20,11 +20,13 @@
#include "genericscriptedconfig.h"
#include "config-kwin.h"
#include "uitranslator.h"
#include <kwineffects_interface.h>
#include <KAboutData>
#define TRANSLATION_DOMAIN "kwin_scripting"
#include <KLocalizedString>
#include <kconfigloader.h>
#include <KDesktopFile>
#include <QFile>
#include <QLabel>
@ -48,7 +50,9 @@ QObject *GenericScriptedConfigFactory::create(const char *iface, QWidget *parent
GenericScriptedConfig::GenericScriptedConfig(const QString &componentName, const QString &keyword, QWidget *parent, const QVariantList &args)
: KCModule(KAboutData::pluginData(componentName), parent, args)
, m_packageName(keyword)
, m_translator(new KLocalizedTranslator(this))
{
QCoreApplication::instance()->installTranslator(m_translator);
}
GenericScriptedConfig::~GenericScriptedConfig()
@ -82,10 +86,25 @@ void GenericScriptedConfig::createUi()
KConfigLoader *configLoader = new KConfigLoader(cg, &xmlFile, this);
// load the ui file
QUiLoader *loader = new QUiLoader(this);
loader->setLanguageChangeEnabled(true);
QFile uiFile(uiPath);
// try getting a translation domain
const QString metaDataPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
QStringLiteral(KWIN_NAME"/%1/%2/metadata.desktop").arg(typeName()).arg(m_packageName));
if (!metaDataPath.isNull()) {
KDesktopFile metaData(metaDataPath);
m_translator->setTranslationDomain(metaData.desktopGroup().readEntry("X-KWin-Config-TranslationDomain", QString()));
}
uiFile.open(QFile::ReadOnly);
QWidget *customConfigForm = loader->load(&uiFile, this);
m_translator->addContextToMonitor(customConfigForm->objectName());
uiFile.close();
// send a custom event to the translator to retranslate using our translator
QEvent le(QEvent::LanguageChange);
QCoreApplication::sendEvent(customConfigForm, &le);
layout->addWidget(customConfigForm);
addConfig(configLoader, customConfigForm);
}

View file

@ -28,6 +28,8 @@
namespace KWin
{
class KLocalizedTranslator;
class GenericScriptedConfigFactory : public KPluginFactory
{
Q_OBJECT
@ -58,6 +60,7 @@ protected:
private:
QString m_packageName;
KLocalizedTranslator *m_translator;
};
class ScriptedEffectConfig : public GenericScriptedConfig