From: Michael Brasser Date: Wed, 27 Jul 2011 05:30:40 +0000 (+1000) Subject: Fix translation context for QML files. X-Git-Tag: qt-v5.0.0-alpha1~2039 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=aa2d36a3d7405c3130b27b41b0e3941ca466f3cd;p=profile%2Fivi%2Fqtdeclarative.git Fix translation context for QML files. Use the base file name as done in QtQuick 1, rather than using the entire path. This also fixes QTBUG-17255 for QtQuick 2. Change-Id: Ia27f6539f82d6caf6e7060b89ff1996d42ffb9cb Reviewed-on: http://codereview.qt.nokia.com/2246 Reviewed-by: Qt Sanity Bot Reviewed-by: Michael Brasser --- diff --git a/src/declarative/qml/v8/qv8engine.cpp b/src/declarative/qml/v8/qv8engine.cpp index b8a1bc4..2dd52b0 100644 --- a/src/declarative/qml/v8/qv8engine.cpp +++ b/src/declarative/qml/v8/qv8engine.cpp @@ -1587,7 +1587,10 @@ v8::Handle QV8Engine::qsTr(const v8::Arguments &args) QV8Engine *v8engine = V8ENGINE(); QDeclarativeContextData *ctxt = v8engine->callingContext(); - QString context = ctxt->url.toString(); + QString path = ctxt->url.toString(); + int lastSlash = path.lastIndexOf(QLatin1Char('/')); + QString context = (lastSlash > -1) ? path.mid(lastSlash + 1, path.length()-lastSlash-5) : QString(); + QString text = v8engine->toString(args[0]); QString comment; if (args.Length() > 1) diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro index 183c920..43a2a68 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/declarative/declarative.pro @@ -19,6 +19,7 @@ PUBLICTESTS += \ qdeclarativemoduleplugin \ qdeclarativepixmapcache \ qdeclarativeqt \ + qdeclarativetranslation \ qdeclarativexmlhttprequest PRIVATETESTS += \ diff --git a/tests/auto/declarative/qdeclarativetranslation/data/translation.qrc b/tests/auto/declarative/qdeclarativetranslation/data/translation.qrc new file mode 100644 index 0000000..2e2d0a0 --- /dev/null +++ b/tests/auto/declarative/qdeclarativetranslation/data/translation.qrc @@ -0,0 +1,6 @@ + + + translation.qml + qml_fr.qm + + diff --git a/tests/auto/declarative/qdeclarativetranslation/qdeclarativetranslation.pro b/tests/auto/declarative/qdeclarativetranslation/qdeclarativetranslation.pro index c970a0c..b6f0df2 100644 --- a/tests/auto/declarative/qdeclarativetranslation/qdeclarativetranslation.pro +++ b/tests/auto/declarative/qdeclarativetranslation/qdeclarativetranslation.pro @@ -3,6 +3,7 @@ contains(QT_CONFIG,declarative): QT += declarative macx:CONFIG -= app_bundle SOURCES += tst_qdeclarativetranslation.cpp +RESOURCES += data/translation.qrc symbian: { importFiles.files = data diff --git a/tests/auto/declarative/qdeclarativetranslation/tst_qdeclarativetranslation.cpp b/tests/auto/declarative/qdeclarativetranslation/tst_qdeclarativetranslation.cpp index 5b88b54..109b98b 100644 --- a/tests/auto/declarative/qdeclarativetranslation/tst_qdeclarativetranslation.cpp +++ b/tests/auto/declarative/qdeclarativetranslation/tst_qdeclarativetranslation.cpp @@ -43,7 +43,6 @@ #include #include #include -#include #ifdef Q_OS_SYMBIAN // In Symbian OS test data is located in applications private dir @@ -59,6 +58,7 @@ public: private slots: void translation(); void idTranslation(); + void translationInQrc(); }; inline QUrl TEST_FILE(const QString &filename) @@ -108,6 +108,30 @@ void tst_qdeclarativetranslation::idTranslation() delete object; } +void tst_qdeclarativetranslation::translationInQrc() +{ + QTranslator translator; + translator.load(":/qml_fr.qm"); + QApplication::installTranslator(&translator); + + QDeclarativeEngine engine; + QDeclarativeComponent component(&engine, QUrl("qrc:/translation.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("basic").toString(), QLatin1String("bonjour")); + QCOMPARE(object->property("basic2").toString(), QLatin1String("au revoir")); + QCOMPARE(object->property("disambiguation").toString(), QLatin1String("salut")); + QCOMPARE(object->property("disambiguation2").toString(), QString::fromUtf8("\xc3\xa0 plus tard")); + QCOMPARE(object->property("noop").toString(), QLatin1String("bonjour")); + QCOMPARE(object->property("noop2").toString(), QLatin1String("au revoir")); + QCOMPARE(object->property("singular").toString(), QLatin1String("1 canard")); + QCOMPARE(object->property("plural").toString(), QLatin1String("2 canards")); + + QApplication::removeTranslator(&translator); + delete object; +} + QTEST_MAIN(tst_qdeclarativetranslation) #include "tst_qdeclarativetranslation.moc"