Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, June 2007
QtScript vs. I18N, lupdate and scripts, does it work?


Message 1 in thread

Hi everyone,

I read this group on daily basis now.. and it is a very good support 
(haven't tried the trolltech support yet.. :-) )...

Now my first question. I hope I'm able to help other people in return later.

Is it possible to combine the QT I18N support with QtScript?

In detail:
* Is it possible to use tr/translate in a script?
* Is it possible to integrate a script in the lupdate process?

Any ideas? :-)

Greetings

Niklas

-- 
 [ signature omitted ] 

Message 2 in thread

I have done similar things with QSA.

I have used global tr function (exported public slot in theobject
representing scripting context) which basically just called
QObject::tr

Then I used simple perl script, ran on all script files like this:

#!/bin/sh
grep "tr(" scripts/d*.qs *.qs | ./trans-qs.pl >>.menu-trans.h
lupdate pdfedit.pro
lrelease lang/*.ts

(this is excerpt from larger script, which automatically
generated/refreshed translations, generated empty translation
template, generated menu translations loaded from external files, etc
... stuff a bit irrelevant to the question)

The script itself can be downloaded at:
http://pdfedit.cvs.sourceforge.net/*checkout*/pdfedit/pdfedit/src/gui/trans-qs.pl?revision=1.2

This produced "dummy" header .menu-trans.h with all needed translations

Then I added following lines into the .pro file, so the dummy header
would be parsed by lupdate (the header itself is actually not included
in any file at all):

#Dummy header file for menu translation, needed by lupdate
exists( .menu-trans.h  ) {
 HEADERS += .menu-trans.h
}

Martin Petricek

On 6/26/07, Niklas Hofmann <n.hofmann@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> Hi everyone,
>
> I read this group on daily basis now.. and it is a very good support
> (haven't tried the trolltech support yet.. :-) )...
>
> Now my first question. I hope I'm able to help other people in return later.
>
> Is it possible to combine the QT I18N support with QtScript?
>
> In detail:
> * Is it possible to use tr/translate in a script?
> * Is it possible to integrate a script in the lupdate process?
>
> Any ideas? :-)
>
> Greetings
>
> Niklas
>
> --
> Jupiter GmbH
> Bahnhofstraße 48-50
> D-21614 Buxtehude
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

--
 [ signature omitted ] 

Message 3 in thread

Hi Martin,

thank you for your help! The perl script is a very good idea!

Combined with the answer of QT support I have a solution (tr-function in 
main.cpp, attachment).

Greetings

Niklas


BH schrieb:
> I have done similar things with QSA.
> 
> I have used global tr function (exported public slot in theobject
> representing scripting context) which basically just called
> QObject::tr
> 
> Then I used simple perl script, ran on all script files like this:
> 
> #!/bin/sh
> grep "tr(" scripts/d*.qs *.qs | ./trans-qs.pl >>.menu-trans.h
> lupdate pdfedit.pro
> lrelease lang/*.ts
> 
> (this is excerpt from larger script, which automatically
> generated/refreshed translations, generated empty translation
> template, generated menu translations loaded from external files, etc
> ... stuff a bit irrelevant to the question)
> 
> The script itself can be downloaded at:
> http://pdfedit.cvs.sourceforge.net/*checkout*/pdfedit/pdfedit/src/gui/trans-qs.pl?revision=1.2 
> 
> 
> This produced "dummy" header .menu-trans.h with all needed translations
> 
> Then I added following lines into the .pro file, so the dummy header
> would be parsed by lupdate (the header itself is actually not included
> in any file at all):
> 
> #Dummy header file for menu translation, needed by lupdate
> exists( .menu-trans.h  ) {
> HEADERS += .menu-trans.h
> }
> 
> Martin Petricek
> 
> On 6/26/07, Niklas Hofmann <n.hofmann@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>> Hi everyone,
>>
>> I read this group on daily basis now.. and it is a very good support
>> (haven't tried the trolltech support yet.. :-) )...
>>
>> Now my first question. I hope I'm able to help other people in return 
>> later.
>>
>> Is it possible to combine the QT I18N support with QtScript?
>>
>> In detail:
>> * Is it possible to use tr/translate in a script?
>> * Is it possible to integrate a script in the lupdate process?
>>
>> Any ideas? :-)
>>
>> Greetings
>>
>> Niklas
>>
>> -- 
>> Jupiter GmbH
>> Bahnhofstraße 48-50
>> D-21614 Buxtehude
>>
>> -- 
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
>> "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>>
>>
> 
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

#include <QApplication>
#include <QApplication>
#include <QVBoxLayout>
#include <QMainWindow>
#include <QWidget>
#include <QCoreApplication>
#include <QScriptEngine>
#include <QDebug>
#include <QScriptValue>
#include <QScriptContext>
#include <QString>
#include <QByteArray>
#include <QObject>
#include <QTranslator>


static const char *script = "print(\"foo\");\n"
                            "print(tr(\"foo\", \"Foo\"));\n";

QScriptValue tr(QScriptContext *context, QScriptEngine *engine)
{
    if (context->argumentCount() < 0 || context->argumentCount() > 2) {
        context->throwError("Expected one or two args, got " + QString::number(context->argumentCount()));
        return QScriptValue();
    }
    QScriptValue val = context->argument(0);
    QByteArray string;
    if (!val.isString()) {
        context->throwError("Expected arg one to be a string");
        return QScriptValue();
    } else {
        string = val.toString().toLatin1();
    }

    QByteArray cont;
    if (context->argumentCount() > 1) {
        QScriptValue arg2 = context->argument(1);
        if (!arg2.isString()) {
            context->throwError("Expected arg two to be a string");
            return QScriptValue();
        }
        cont = arg2.toString().toLatin1();
    }
    const QString ret = QApplication::translate(cont.constData(), string.constData());
    return QScriptValue(engine, ret);
}

class Foo : public QObject
{
    Q_OBJECT
public:
    Foo(QObject *parent = 0)
        : QObject(parent)
    {
        tr("foo");
    }
};




int main(int argc, char **argv)
{
    QCoreApplication a(argc, argv);
    QTranslator translator;
    translator.load("other");
    a.installTranslator(&translator);
    QScriptEngine engine;
    QScriptValue val = engine.newFunction(tr);
    engine.globalObject().setProperty("tr", val);
    engine.evaluate(script);
    if (engine.hasUncaughtException())
        qDebug() << engine.uncaughtException().toString();
    return 0;
}