[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: KDE/kdelibs/kate/script
From: Christoph Cullmann <cullmann () kde ! org>
Date: 2010-06-07 17:34:48
Message-ID: 20100607173448.38352AC8CD () svn ! kde ! org
[Download RAW message or body]
SVN commit 1135588 by cullmann:
dhaumann:
export KDE's translation functions like in Kross
based on code from kdelibs/kross/modules/translation.cpp
Dag: Please quickly review and give your OK with the license, as this
is only LGPL v2/v3 in Kate Part (iiuc, there is no conflict anyway)
CCMAIL: danders@get2net.dk
M +160 -3 katescript.cpp
--- trunk/KDE/kdelibs/kate/script/katescript.cpp #1135587:1135588
@@ -1,6 +1,6 @@
// This file is part of the KDE libraries
// Copyright (C) 2008 Paul Giannaros <paul@giannaros.org>
-// Copyright (C) 2009 Dominik Haumann <dhaumann kde org>
+// Copyright (C) 2009, 2010 Dominik Haumann <dhaumann kde org>
// Copyright (C) 2010 Joseph Wenninger <jowenn@kde.org>
//
// This library is free software; you can redistribute it and/or
@@ -36,6 +36,7 @@
#include <kdebug.h>
#include <klocale.h>
#include <kstandarddirs.h>
+#include <klocalizedstring.h>
//BEGIN conversion functions for Cursors and Ranges
/** Converstion function from KTextEditor::Cursor to QtScript cursor */
@@ -78,7 +79,8 @@
namespace Kate {
namespace Script {
- QScriptValue debug(QScriptContext *context, QScriptEngine *engine) {
+ QScriptValue debug(QScriptContext *context, QScriptEngine *engine)
+ {
QStringList message;
for(int i = 0; i < context->argumentCount(); ++i) {
message << context->argument(i).toString();
@@ -88,11 +90,158 @@
return engine->nullValue();
}
+//BEGIN code adapted from kdelibs/kross/modules/translation.cpp
+ /// helper function to do the substitution from QtScript > QVariant > real \
values + KLocalizedString substituteArguments( const KLocalizedString &kls, const \
QVariantList &arguments, int max = 99 ) + {
+ KLocalizedString ls = kls;
+ int cnt = qMin( arguments.count(), max ); // QString supports max 99
+ for ( int i = 0; i < cnt; ++i ) {
+ QVariant arg = arguments[i];
+ switch ( arg.type() ) {
+ case QVariant::Int: ls = ls.subs(arg.toInt()); break;
+ case QVariant::UInt: ls = ls.subs(arg.toUInt()); break;
+ case QVariant::LongLong: ls = ls.subs(arg.toLongLong()); break;
+ case QVariant::ULongLong: ls = ls.subs(arg.toULongLong()); break;
+ case QVariant::Double: ls = ls.subs(arg.toDouble()); break;
+ default: ls = ls.subs(arg.toString()); break;
}
}
+ return ls;
+ }
+ /// i18n("text", arguments [optional])
+ QScriptValue i18n( QScriptContext *context, QScriptEngine *engine )
+ {
+ Q_UNUSED(engine)
+ QString text;
+ QVariantList args;
+ const int argCount = context->argumentCount();
+ if (argCount == 0) {
+ kWarning(13050) << "wrong usage of i18n:" << \
context->backtrace().join("\n\t"); + }
+ if (argCount > 0) {
+ text = context->argument(0).toString();
+ }
+
+ for (int i = 1; i < argCount; ++i) {
+ args << context->argument(i).toVariant();
+ }
+
+ KLocalizedString ls = ki18n(text.toUtf8());
+ return substituteArguments( ls, args ).toString();
+ }
+
+ /// i18nc("context", "text", arguments [optional])
+ QScriptValue i18nc( QScriptContext *context, QScriptEngine *engine )
+ {
+ Q_UNUSED(engine)
+ QString text;
+ QString textContext;
+ QVariantList args;
+ const int argCount = context->argumentCount();
+
+ if (argCount < 2) {
+ kWarning(13050) << "wrong usage of i18nc:" << \
context->backtrace().join("\n\t"); + }
+
+ if (argCount > 0) {
+ textContext = context->argument(0).toString();
+ }
+
+ if (argCount > 1) {
+ text = context->argument(1).toString();
+ }
+
+ for (int i = 2; i < argCount; ++i) {
+ args << context->argument(i).toVariant();
+ }
+
+ KLocalizedString ls = ki18nc(textContext.toUtf8(), text.toUtf8());
+ return substituteArguments( ls, args ).toString();
+ }
+
+ /// i18n("singular", "plural", number, arguments [optional])
+ QScriptValue i18np( QScriptContext *context, QScriptEngine *engine )
+ {
+ Q_UNUSED(engine)
+ QString trSingular;
+ QString trPlural;
+ int number;
+ QVariantList args;
+ const int argCount = context->argumentCount();
+
+ if (argCount < 3) {
+ kWarning(13050) << "wrong usage of i18np:" << \
context->backtrace().join("\n\t"); + }
+
+ if (argCount > 0) {
+ trSingular = context->argument(0).toString();
+ }
+
+ if (argCount > 1) {
+ trPlural = context->argument(1).toString();
+ }
+
+ if (argCount > 2) {
+ number = context->argument(2).toInt32();
+ }
+
+ for (int i = 3; i < argCount; ++i) {
+ args << context->argument(i).toVariant();
+ }
+
+ KLocalizedString ls = ki18np(trSingular.toUtf8(), \
trPlural.toUtf8()).subs(number); + return substituteArguments( ls, args, 98 \
).toString(); + }
+
+ /// i18n("context", "singular", "plural", number, arguments [optional])
+ QScriptValue i18ncp( QScriptContext *context, QScriptEngine *engine )
+ {
+ Q_UNUSED(engine)
+ QString trContext;
+ QString trSingular;
+ QString trPlural;
+ int number;
+ QVariantList args;
+ const int argCount = context->argumentCount();
+
+ if (argCount < 4) {
+ kWarning(13050) << "wrong usage of i18ncp:" << \
context->backtrace().join("\n\t"); + }
+
+ if (argCount > 0) {
+ trContext = context->argument(0).toString();
+ }
+
+ if (argCount > 1) {
+ trSingular = context->argument(1).toString();
+ }
+
+ if (argCount > 2) {
+ trPlural = context->argument(2).toString();
+ }
+
+ if (argCount > 3) {
+ number = context->argument(3).toInt32();
+ }
+
+ for (int i = 4; i < argCount; ++i) {
+ args << context->argument(i).toVariant();
+ }
+
+ KLocalizedString ls = ki18ncp(trContext.toUtf8(), trSingular.toUtf8(), \
trPlural.toUtf8()).subs( number ); + return substituteArguments( ls, args, 98 \
).toString(); + }
+//END code adapted from kdelibs/kross/modules/translation.cpp
+
+ }
+}
+
+
+
bool KateScript::s_scriptingApiLoaded = false;
void KateScript::reloadScriptingApi()
@@ -279,12 +428,20 @@
}
-void KateScript::initEngine() {
+void KateScript::initEngine()
+{
// set the view/document objects as necessary
m_engine->globalObject().setProperty("document", m_engine->newQObject(m_document = \
new KateScriptDocument())); m_engine->globalObject().setProperty("view", \
m_engine->newQObject(m_view = new KateScriptView()));
+ // export debug scripts
m_engine->globalObject().setProperty("debug", \
m_engine->newFunction(Kate::Script::debug)); +
+ // export translation functions
+ m_engine->globalObject().setProperty("i18n", \
m_engine->newFunction(Kate::Script::i18n)); + \
m_engine->globalObject().setProperty("i18nc", \
m_engine->newFunction(Kate::Script::i18nc)); + \
m_engine->globalObject().setProperty("i18ncp", \
m_engine->newFunction(Kate::Script::i18ncp)); + \
m_engine->globalObject().setProperty("i18np", \
m_engine->newFunction(Kate::Script::i18np)); }
bool KateScript::setView(KateView *view)
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic