Commit 512ec3d6 authored by Volker Krause's avatar Volker Krause
Browse files

KDE integration plugin for QML. So far it only provides access to i18n

methods, slightly ugly since method overloading doesn't seem to be
possible.

svn path=/trunk/playground/pim/akonadi/kde-qml/; revision=1101913
parent 2eb60616
kde4_add_plugin( kdeqmlplugin WITH_PREFIX
kdeintegration.cpp
kdeintegrationplugin.cpp
)
target_link_libraries( kdeqmlplugin ${KDE4_KDECORE_LIBS} QtDeclarative )
install( FILES qmldir DESTINATION ${PLUGIN_INSTALL_DIR}/imports/org/kde )
install( TARGETS kdeqmlplugin DESTINATION ${PLUGIN_INSTALL_DIR}/imports/org/kde )
/*
Copyright (c) 2010 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "kdeintegration.h"
#include <KDebug>
#include <KLocale>
static QString translate( const KLocalizedString &_string, const QVariantList &args = QVariantList() )
{
KLocalizedString string( _string );
foreach ( const QVariant &arg, args ) {
switch ( arg.type() ) {
case QVariant::Char:
string = string.subs( arg.toChar() );
break;
case QVariant::Int:
string = string.subs( arg.toInt() );
break;
case QVariant::UInt:
string = string.subs( arg.toUInt() );
break;
case QVariant::LongLong:
string = string.subs( arg.toLongLong() );
break;
case QVariant::ULongLong:
string = string.subs( arg.toULongLong() );
break;
case QVariant::Double:
string = string.subs( arg.toDouble() );
break;
case QVariant::String:
string = string.subs( arg.toString() );
break;
default:
kWarning() << "Unknown i18n argument type:" << arg;
}
}
return string.toString();
}
KDEIntegration::KDEIntegration( QObject *parent ) : QObject( parent )
{
}
QString KDEIntegration::i18n( const QString &message )
{
return translate( ki18n( message.toLatin1().constData() ) );
}
QString KDEIntegration::i18na( const QString &message, const QVariantList &args )
{
return translate( ki18n( message.toLatin1().constData() ), args );
}
QString KDEIntegration::i18nc( const QString &context, const QString &message )
{
return translate( ki18nc( context.toLatin1().constData(), message.toLatin1().constData() ) );
}
QString KDEIntegration::i18nca( const QString &context, const QString &message, const QVariantList &args )
{
return translate( ki18nc( context.toLatin1().constData(), message.toLatin1().constData() ), args );
}
QString KDEIntegration::i18np( const QString &singular, const QString &plural, int amount )
{
return translate( ki18np( singular.toLatin1().constData(), plural.toLatin1().constData() ), QVariantList() << QVariant( amount ) );
}
QString KDEIntegration::i18npa( const QString &singular, const QString &plural, int amount, const QVariantList &args )
{
QVariantList _args;
_args.append( QVariant( amount ) );
_args.append( args );
return translate( ki18np( singular.toLatin1().constData(), plural.toLatin1().constData() ), _args );
}
QString KDEIntegration::i18ncp( const QString &context, const QString &singular, const QString &plural, int amount )
{
return translate( ki18ncp( context.toLatin1().constData(), singular.toLatin1().constData(), plural.toLatin1().constData() ), QVariantList() << QVariant( amount ) );
}
QString KDEIntegration::i18ncpa( const QString &context, const QString &singular, const QString &plural, int amount, const QVariantList &args )
{
QVariantList _args;
_args.append( QVariant( amount ) );
_args.append( args );
return translate( ki18ncp( context.toLatin1().constData(), singular.toLatin1().constData(), plural.toLatin1().constData() ), _args );
}
#include "kdeintegration.moc"
/*
Copyright (c) 2010 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef KDEINTEGRATION_H
#define KDEINTEGRATION_H
#include <QObject>
#include <QStringList>
#include <QVariant>
class KDEIntegration : public QObject
{
Q_OBJECT
public:
explicit KDEIntegration(QObject* parent = 0);
public slots:
QString i18n( const QString &message );
QString i18na( const QString &message, const QVariantList &args );
QString i18nc( const QString &context, const QString &message );
QString i18nca( const QString &context, const QString &message, const QVariantList &args );
QString i18np( const QString &singular, const QString &plural, int amount );
QString i18npa( const QString &singular, const QString &plural, int amount, const QVariantList &args );
QString i18ncp( const QString &context, const QString &singular, const QString &plural, int amount );
QString i18ncpa( const QString &context, const QString &singular, const QString &plural, int amount, const QVariantList &args );
};
#endif
/*
Copyright (c) 2010 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "kdeintegrationplugin.h"
#include "kdeintegration.h"
#include <kdebug.h>
#include <QtDeclarative/qdeclarative.h>
#include <qdeclarativecontext.h>
#include <qdeclarativeengine.h>
KDEIntegrationPlugin::KDEIntegrationPlugin(QObject* parent) : QDeclarativeExtensionPlugin( parent )
{
qDebug() << Q_FUNC_INFO;
kDebug();
}
void KDEIntegrationPlugin::registerTypes(const char* uri)
{
kDebug() << uri;
// qmlRegisterType<KDEIntegration>( uri, 4, 5, "KDE" );
}
void KDEIntegrationPlugin::initializeEngine(QDeclarativeEngine *engine, const char* uri)
{
kDebug() << engine << uri;
engine->rootContext()->setContextProperty( QLatin1String("KDE"), new KDEIntegration( engine ) );
}
#include "kdeintegrationplugin.moc"
Q_EXPORT_PLUGIN2( kdeintegrationplugin, KDEIntegrationPlugin )
/*
Copyright (c) 2010 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef KDEDECLARATIVEEXTENSIONPLUGIN_H
#define KDEDECLARATIVEEXTENSIONPLUGIN_H
#include <qdeclarativeextensionplugin.h>
#include <kglobal.h>
class KDE_EXPORT KDEIntegrationPlugin : public QDeclarativeExtensionPlugin
{
Q_OBJECT
public:
explicit KDEIntegrationPlugin(QObject* parent = 0);
virtual void registerTypes(const char* uri);
virtual void initializeEngine(QDeclarativeEngine* engine, const char* uri);
};
#endif
plugin kdeqmlplugin
import Qt 4.6
import org.kde 4.5
Rectangle {
width: 100; height: 100; color: "blue"
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
console.log( "*** begin i18n test ***" );
console.log( KDE.i18n( "single message" ) );
console.log( KDE.i18na( "single message with arguments %1 %2", ["bla", "foo"] ) );
console.log( KDE.i18nc( "context", "message with context" ) );
console.log( KDE.i18nca( "context", "message with context and arguments %1 %2 %3", [5, 3.1415, "string"] ) );
console.log( KDE.i18np( "singular", "%1 plural", 1 ) );
console.log( KDE.i18np( "singluar", "%1 plural", 2 ) );
console.log( KDE.i18npa( "singluar with arg %2", "%1 plural with arg %2", 1, ["bla"] ) );
console.log( KDE.i18npa( "singluar with arg %2", "%1 plural with arg %2", 2, ["bla"] ) );
console.log( KDE.i18ncp( "context", "singular", "%1 plural", 1 ) );
console.log( KDE.i18ncp( "context", "singluar", "%1 plural", 2 ) );
console.log( KDE.i18ncpa( "context", "singluar with arg %2", "%1 plural with arg %2", 1, ["bla"] ) );
console.log( KDE.i18ncpa( "context", "singluar with arg %2", "%1 plural with arg %2", 2, ["bla"] ) );
console.log( "*** end i18n test ***" );
}
}
Text {
text: KDE.i18n( "Click to run test!" );
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment