aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKai Koehne <kai.koehne@theqtcompany.com>2014-12-04 14:19:09 +0100
committerJani Heikkinen <jani.heikkinen@theqtcompany.com>2014-12-05 05:50:27 +0100
commit2ab6b863f4493acf5256de0027f1e3daa0fdd038 (patch)
treed8af24dba9bbaff3dd642ace4491b9125d78a2cf
parent2509e9b7bcb9f06351016433244e529436f380cc (diff)
Add string::arg method in installTranslatorFunctions
QJSEngine::installTranslatorFunctions install the translator functions to any given JS object. However, the custom string::arg() method is only added in qqmlbuiltinfunctions.cpp, making the use of qsTr() in other pure-JS programs quite hard. Task-number: QTBUG-43113 Change-Id: Ia9ed97a4c07a4d167c792f3ea13e4f6e96c97423 Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
-rw-r--r--src/qml/jsapi/qjsengine.cpp6
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp5
2 files changed, 11 insertions, 0 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp
index 0d2b394cd6..d7f45b56e0 100644
--- a/src/qml/jsapi/qjsengine.cpp
+++ b/src/qml/jsapi/qjsengine.cpp
@@ -243,6 +243,8 @@ void QJSEngine::collectGarbage()
\row \li QT_TRID_NOOP() \li QT_TRID_NOOP()
\endtable
+ It also adds an arg() method to the string prototype.
+
\sa {Internationalization with Qt}
*/
void QJSEngine::installTranslatorFunctions(const QJSValue &object)
@@ -260,6 +262,10 @@ void QJSEngine::installTranslatorFunctions(const QJSValue &object)
obj->defineDefaultProperty(QStringLiteral("QT_TR_NOOP"), QV4::GlobalExtensions::method_qsTrNoOp);
obj->defineDefaultProperty(QStringLiteral("qsTrId"), QV4::GlobalExtensions::method_qsTrId);
obj->defineDefaultProperty(QStringLiteral("QT_TRID_NOOP"), QV4::GlobalExtensions::method_qsTrIdNoOp);
+
+ // string prototype extension
+ v4->stringObjectClass->prototype->defineDefaultProperty(QStringLiteral("arg"),
+ QV4::GlobalExtensions::method_string_arg);
#endif
}
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index 741fa9f04d..c43bd29614 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -3190,6 +3190,11 @@ void tst_QJSEngine::installTranslatorFunctions()
QVERIFY(ret.isString());
QCOMPARE(ret.toString(), QString::fromLatin1("foo"));
}
+ {
+ QJSValue ret = eng.evaluate("qsTr('%1').arg('foo')");
+ QVERIFY(ret.isString());
+ QCOMPARE(ret.toString(), QString::fromLatin1("foo"));
+ }
QVERIFY(eng.evaluate("QT_TRID_NOOP()").isUndefined());
}