Fix translation context for QML files.
authorMichael Brasser <michael.brasser@nokia.com>
Wed, 27 Jul 2011 05:30:40 +0000 (15:30 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 28 Jul 2011 09:27:26 +0000 (11:27 +0200)
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 <qt_sanity_bot@ovi.com>
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
src/declarative/qml/v8/qv8engine.cpp
tests/auto/declarative/declarative.pro
tests/auto/declarative/qdeclarativetranslation/data/translation.qrc [new file with mode: 0644]
tests/auto/declarative/qdeclarativetranslation/qdeclarativetranslation.pro
tests/auto/declarative/qdeclarativetranslation/tst_qdeclarativetranslation.cpp

index b8a1bc4..2dd52b0 100644 (file)
@@ -1587,7 +1587,10 @@ v8::Handle<v8::Value> 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)
index 183c920..43a2a68 100644 (file)
@@ -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 (file)
index 0000000..2e2d0a0
--- /dev/null
@@ -0,0 +1,6 @@
+<RCC>
+    <qresource prefix="/">
+        <file>translation.qml</file>
+        <file>qml_fr.qm</file>
+    </qresource>
+</RCC>
index c970a0c..b6f0df2 100644 (file)
@@ -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
index 5b88b54..109b98b 100644 (file)
@@ -43,7 +43,6 @@
 #include <QDeclarativeEngine>
 #include <QDeclarativeComponent>
 #include <QTranslator>
-#include <QDebug>
 
 #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"