From 9415afbd2ca6a4b136ecdc4cb4936e15cc523229 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Wed, 14 Apr 2010 18:49:35 +0200 Subject: QtScript: Add tests for translation of multiple scripts Suggested by Olivier. --- tests/auto/qscriptengine/translatable2.js | 9 +++++ .../qscriptengine/translations/translatable_la.qm | Bin 315 -> 678 bytes .../qscriptengine/translations/translatable_la.ts | 32 +++++++++++++++++ tests/auto/qscriptengine/tst_qscriptengine.cpp | 40 +++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 tests/auto/qscriptengine/translatable2.js diff --git a/tests/auto/qscriptengine/translatable2.js b/tests/auto/qscriptengine/translatable2.js new file mode 100644 index 0000000..eee66f1 --- /dev/null +++ b/tests/auto/qscriptengine/translatable2.js @@ -0,0 +1,9 @@ +qsTr("Three"); +qsTranslate("BarContext", "Four"); + +var celebration_strings = [ + QT_TR_NOOP("Happy birthday!"), + QT_TRANSLATE_NOOP("BarContext", "Congratulations!") +]; + +qsTr("Three", "not the same three"); diff --git a/tests/auto/qscriptengine/translations/translatable_la.qm b/tests/auto/qscriptengine/translations/translatable_la.qm index 301cc77..703d0f1 100644 Binary files a/tests/auto/qscriptengine/translations/translatable_la.qm and b/tests/auto/qscriptengine/translations/translatable_la.qm differ diff --git a/tests/auto/qscriptengine/translations/translatable_la.ts b/tests/auto/qscriptengine/translations/translatable_la.ts index 4eed72d..1598f39 100644 --- a/tests/auto/qscriptengine/translations/translatable_la.ts +++ b/tests/auto/qscriptengine/translations/translatable_la.ts @@ -2,6 +2,19 @@ + BarContext + + + Four + Fire + + + + Congratulations! + Gratulerer! + + + FooContext @@ -37,4 +50,23 @@ Farvel + + translatable2 + + + Three + Tre + + + + Happy birthday! + Gratulerer med dagen! + + + + Three + not the same three + Tre andre + + diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index aa06bc9..5713f8e 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -4317,6 +4317,11 @@ void tst_QScriptEngine::installTranslatorFunctions() } } +static QScriptValue callQsTr(QScriptContext *ctx, QScriptEngine *eng) +{ + return eng->globalObject().property("qsTr").call(ctx->thisObject(), ctx->argumentsObject()); +} + void tst_QScriptEngine::translateScript() { QScriptEngine engine; @@ -4364,6 +4369,41 @@ void tst_QScriptEngine::translateScript() QCOMPARE(engine.globalObject().property("qsTr").call( QScriptValue(), QScriptValueList() << "One").toString(), QString::fromLatin1("One")); + // Translate strings from the second script (translatable2.js) + + QString fileName2 = QString::fromLatin1("translatable2.js"); + + QCOMPARE(engine.evaluate("qsTr('Three')", fileName2).toString(), QString::fromLatin1("Tre")); + QCOMPARE(engine.evaluate("qsTr('Happy birthday!')", fileName2).toString(), QString::fromLatin1("Gratulerer med dagen!")); + + // Not translated because translation is only in translatable.js + QCOMPARE(engine.evaluate("qsTr('One')", fileName2).toString(), QString::fromLatin1("One")); + QCOMPARE(engine.evaluate("(function() { return qsTr('One'); })()", fileName2).toString(), QString::fromLatin1("One")); + + // For qsTranslate() the filename shouldn't matter + QCOMPARE(engine.evaluate("qsTranslate('FooContext', 'Two')", fileName2).toString(), QString::fromLatin1("To")); + QCOMPARE(engine.evaluate("qsTranslate('BarContext', 'Congratulations!')", fileName).toString(), QString::fromLatin1("Gratulerer!")); + + // qsTr() should use the innermost filename as context + engine.evaluate("function foo(s) { return bar(s); }", fileName); + engine.evaluate("function bar(s) { return qsTr(s); }", fileName2); + QCOMPARE(engine.evaluate("bar('Three')", fileName2).toString(), QString::fromLatin1("Tre")); + QCOMPARE(engine.evaluate("bar('Three')", fileName).toString(), QString::fromLatin1("Tre")); + QCOMPARE(engine.evaluate("bar('One')", fileName2).toString(), QString::fromLatin1("One")); + + engine.evaluate("function foo(s) { return bar(s); }", fileName2); + engine.evaluate("function bar(s) { return qsTr(s); }", fileName); + QCOMPARE(engine.evaluate("bar('Three')", fileName2).toString(), QString::fromLatin1("Three")); + QCOMPARE(engine.evaluate("bar('One')", fileName).toString(), QString::fromLatin1("En")); + QCOMPARE(engine.evaluate("bar('One')", fileName2).toString(), QString::fromLatin1("En")); + + // Calling qsTr() from a native function + engine.globalObject().setProperty("qsTrProxy", engine.newFunction(callQsTr)); + QCOMPARE(engine.evaluate("qsTrProxy('One')", fileName).toString(), QString::fromLatin1("En")); + QCOMPARE(engine.evaluate("qsTrProxy('One')", fileName2).toString(), QString::fromLatin1("One")); + QCOMPARE(engine.evaluate("qsTrProxy('Three')", fileName).toString(), QString::fromLatin1("Three")); + QCOMPARE(engine.evaluate("qsTrProxy('Three')", fileName2).toString(), QString::fromLatin1("Tre")); + QCoreApplication::instance()->removeTranslator(&translator); } -- cgit v0.12