aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlbuiltinfunctions.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-05-25 15:46:13 +0200
committerUlf Hermann <ulf.hermann@qt.io>2022-06-24 14:47:58 +0200
commit23fdccf7f3d4ace5b0cb9f04d9b941a1235629b8 (patch)
treeae57cde18d0c43ff09e49fa449187b7cc4accde0 /src/qml/qml/qqmlbuiltinfunctions.cpp
parent1b6069798aa7088457eb5e033aa2fa1d809ec3e4 (diff)
QmlCompiler: Inline translation methods
We hardcode them into QQmlJSTypePropagator and QQmlJSCodegenerator for now. This is OK for builtins. Task-number: QTBUG-101387 Change-Id: Ifab46083b3a782f009859ce969c283d5bb2b4e8b Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlbuiltinfunctions.cpp')
-rw-r--r--src/qml/qml/qqmlbuiltinfunctions.cpp77
1 files changed, 40 insertions, 37 deletions
diff --git a/src/qml/qml/qqmlbuiltinfunctions.cpp b/src/qml/qml/qqmlbuiltinfunctions.cpp
index 4702797e74..80bd427970 100644
--- a/src/qml/qml/qqmlbuiltinfunctions.cpp
+++ b/src/qml/qml/qqmlbuiltinfunctions.cpp
@@ -1942,37 +1942,11 @@ ReturnedValue GlobalExtensions::method_qsTranslateNoOp(const FunctionObject *b,
return argv[1].asReturnedValue();
}
-/*!
- \qmlmethod string Qt::qsTr(string sourceText, string disambiguation, int n)
-
- Returns a translated version of \a sourceText, optionally based on a
- \a disambiguation string and value of \a n for strings containing plurals;
- otherwise returns \a sourceText itself if no appropriate translated string
- is available.
-
- If the same \a sourceText is used in different roles within the
- same translation context, an additional identifying string may be passed in
- for \a disambiguation.
-
- Example:
- \snippet qml/qsTr.qml 0
-
- \sa {Internationalization and Localization with Qt Quick}
-*/
-ReturnedValue GlobalExtensions::method_qsTr(const FunctionObject *b, const Value *, const Value *argv, int argc)
+QString GlobalExtensions::currentTranslationContext(ExecutionEngine *engine)
{
- QV4::Scope scope(b);
- if (argc < 1)
- THROW_GENERIC_ERROR("qsTr() requires at least one argument");
- if (!argv[0].isString())
- THROW_GENERIC_ERROR("qsTr(): first argument (sourceText) must be a string");
- if ((argc > 1) && !argv[1].isString())
- THROW_GENERIC_ERROR("qsTr(): second argument (disambiguation) must be a string");
- if ((argc > 2) && !argv[2].isNumber())
- THROW_GENERIC_ERROR("qsTr(): third argument (n) must be a number");
-
QString context;
- CppStackFrame *frame = scope.engine->currentStackFrame;
+ CppStackFrame *frame = engine->currentStackFrame;
+
// The first non-empty source URL in the call stack determines the translation context.
while (frame && context.isEmpty()) {
if (CompiledData::CompilationUnitBase *baseUnit = frame->v4Function->compilationUnit) {
@@ -1992,7 +1966,7 @@ ReturnedValue GlobalExtensions::method_qsTr(const FunctionObject *b, const Value
}
if (context.isEmpty()) {
- if (QQmlRefPointer<QQmlContextData> ctxt = scope.engine->callingQmlContext()) {
+ if (QQmlRefPointer<QQmlContextData> ctxt = engine->callingQmlContext()) {
QString path = ctxt->urlString();
int lastSlash = path.lastIndexOf(QLatin1Char('/'));
int lastDot = path.lastIndexOf(QLatin1Char('.'));
@@ -2001,13 +1975,42 @@ ReturnedValue GlobalExtensions::method_qsTr(const FunctionObject *b, const Value
}
}
- QString text = argv[0].toQStringNoThrow();
- QString comment;
- if (argc > 1)
- comment = argv[1].toQStringNoThrow();
- int n = -1;
- if (argc > 2)
- n = argv[2].toInt32();
+ return context;
+}
+
+/*!
+ \qmlmethod string Qt::qsTr(string sourceText, string disambiguation, int n)
+
+ Returns a translated version of \a sourceText, optionally based on a
+ \a disambiguation string and value of \a n for strings containing plurals;
+ otherwise returns \a sourceText itself if no appropriate translated string
+ is available.
+
+ If the same \a sourceText is used in different roles within the
+ same translation context, an additional identifying string may be passed in
+ for \a disambiguation.
+
+ Example:
+ \snippet qml/qsTr.qml 0
+
+ \sa {Internationalization and Localization with Qt Quick}
+*/
+ReturnedValue GlobalExtensions::method_qsTr(const FunctionObject *b, const Value *, const Value *argv, int argc)
+{
+ QV4::Scope scope(b);
+ if (argc < 1)
+ THROW_GENERIC_ERROR("qsTr() requires at least one argument");
+ if (!argv[0].isString())
+ THROW_GENERIC_ERROR("qsTr(): first argument (sourceText) must be a string");
+ if ((argc > 1) && !argv[1].isString())
+ THROW_GENERIC_ERROR("qsTr(): second argument (disambiguation) must be a string");
+ if ((argc > 2) && !argv[2].isNumber())
+ THROW_GENERIC_ERROR("qsTr(): third argument (n) must be a number");
+
+ const QString context = currentTranslationContext(scope.engine);
+ const QString text = argv[0].toQStringNoThrow();
+ const QString comment = argc > 1 ? argv[1].toQStringNoThrow() : QString();
+ const int n = argc > 2 ? argv[2].toInt32() : -1;
if (QQmlEnginePrivate *ep = (scope.engine->qmlEngine() ? QQmlEnginePrivate::get(scope.engine->qmlEngine()) : nullptr))
if (ep->propertyCapture)