aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@digia.com>2014-06-06 12:42:23 +0200
committerSimon Hausmann <simon.hausmann@digia.com>2014-06-13 09:10:04 +0200
commitcb0a47a48d5dc0ce4f5e8cfa68a39cd4cbfde11c (patch)
tree0691a90251d50b0973a9bccdaa9f59c887315f94 /src/qml/qml/v8/qqmlbuiltinfunctions.cpp
parent4d68155848723a863e59d2ce99142b56c75ab3c6 (diff)
Add support for translations in pure QJSEngine based environments
Re-add the QScriptEngine::addTranslatorFunctions API that brings back qsTr() and friends to pure QJSEngine based environments. The auto-test were also ported where applicable. Change-Id: Ib03e3495ef09eeea9e4c8341061499768caed307 Sanity-Review: Simon Hausmann <simon.hausmann@digia.com> Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Diffstat (limited to 'src/qml/qml/v8/qqmlbuiltinfunctions.cpp')
-rw-r--r--src/qml/qml/v8/qqmlbuiltinfunctions.cpp33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
index a7db7d214e..67e9e80efb 100644
--- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
+++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp
@@ -47,6 +47,7 @@
#include <private/qqmlstringconverters_p.h>
#include <private/qqmllocale_p.h>
#include <private/qv8engine_p.h>
+#include <QFileInfo>
#include <private/qqmlprofilerservice_p.h>
#include <private/qqmlglobal_p.h>
@@ -1709,13 +1710,31 @@ ReturnedValue GlobalExtensions::method_qsTr(CallContext *ctx)
V4THROW_ERROR("qsTr(): third argument (n) must be a number");
QV8Engine *v8engine = ctx->engine->v8Engine;
- QQmlContextData *ctxt = v8engine->callingContext();
-
- QString path = ctxt->url.toString();
- int lastSlash = path.lastIndexOf(QLatin1Char('/'));
- int lastDot = path.lastIndexOf(QLatin1Char('.'));
- int length = lastDot - (lastSlash + 1);
- QString context = (lastSlash > -1) ? path.mid(lastSlash + 1, (length > -1) ? length : -1) : QString();
+ QString context;
+ if (QQmlContextData *ctxt = v8engine->callingContext()) {
+ QString path = ctxt->url.toString();
+ int lastSlash = path.lastIndexOf(QLatin1Char('/'));
+ int lastDot = path.lastIndexOf(QLatin1Char('.'));
+ int length = lastDot - (lastSlash + 1);
+ context = (lastSlash > -1) ? path.mid(lastSlash + 1, (length > -1) ? length : -1) : QString();
+ } else if (QV4::ExecutionContext *parentCtx = ctx->parent) {
+ // The first non-empty source URL in the call stack determines the translation context.
+ while (parentCtx && context.isEmpty()) {
+ if (QV4::CompiledData::CompilationUnit *unit = parentCtx->compilationUnit) {
+ QString fileName = unit->fileName();
+ QUrl url(unit->fileName());
+ if (url.isValid() && url.isRelative()) {
+ context = url.fileName();
+ } else {
+ context = QQmlFile::urlToLocalFileOrQrc(fileName);
+ if (context.isEmpty() && fileName.startsWith(QLatin1String(":/")))
+ context = fileName;
+ }
+ context = QFileInfo(context).baseName();
+ }
+ parentCtx = parentCtx->parent;
+ }
+ }
QString text = ctx->callData->args[0].toQStringNoThrow();
QString comment;