summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2018-03-28 11:12:57 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2018-03-29 15:11:46 +0000
commitf1c59c4e73236f8a93489776f3746d72dde94cc4 (patch)
tree337e34c02aa9f7ff7b41e3316a624ecb1b1c7350
parentb34f81532688f6b3809e17a9bbd98f72015fe915 (diff)
downloadqttools-f1c59c4e73236f8a93489776f3746d72dde94cc4.tar.gz
Add support for translation function aliases that are member expressions
When traversing the AST for call expressions, it may happen that the base of the call expression is a field member expression instead of a plain identifier. For example with CustomTranslator.myTr() the base is a field member, with an identifier as base and myTr as name. We can construct a qualified name from that quite easily and this way support qualified tr aliases in qml files. Task-number: QTBUG-67278 Change-Id: I106243d8eed6d8cec75819a44942cf750de2829a Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
-rw-r--r--src/linguist/lupdate/qdeclarative.cpp14
-rw-r--r--tests/auto/linguist/lupdate/testdata/good/tr_function_alias/lupdatecmd2
-rw-r--r--tests/auto/linguist/lupdate/testdata/good/tr_function_alias/main.qml4
-rw-r--r--tests/auto/linguist/lupdate/testdata/good/tr_function_alias/project.ts.result8
4 files changed, 25 insertions, 3 deletions
diff --git a/src/linguist/lupdate/qdeclarative.cpp b/src/linguist/lupdate/qdeclarative.cpp
index 11d37259d..40af15566 100644
--- a/src/linguist/lupdate/qdeclarative.cpp
+++ b/src/linguist/lupdate/qdeclarative.cpp
@@ -90,10 +90,20 @@ protected:
void endVisit(AST::CallExpression *node)
{
- if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(node->base)) {
+ QString name;
+ AST::ExpressionNode *base = node->base;
+
+ while (base && base->kind == AST::Node::Kind_FieldMemberExpression) {
+ auto memberExpr = static_cast<AST::FieldMemberExpression *>(base);
+ name.prepend(memberExpr->name);
+ name.prepend(QLatin1Char('.'));
+ base = memberExpr->base;
+ }
+
+ if (AST::IdentifierExpression *idExpr = AST::cast<AST::IdentifierExpression *>(base)) {
processComments(idExpr->identifierToken.begin());
- const QString name = idExpr->name.toString();
+ name = idExpr->name.toString() + name;
const int identLineNo = idExpr->identifierToken.startLine;
switch (trFunctionAliasManager.trFunctionByName(name)) {
case TrFunctionAliasManager::Function_qsTr:
diff --git a/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/lupdatecmd b/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/lupdatecmd
index 7996a444a..e2f91b5f1 100644
--- a/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/lupdatecmd
+++ b/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/lupdatecmd
@@ -1 +1 @@
-lupdate main.cpp -tr-function-alias QT_TRANSLATE_NOOP+=QT_TRANSLATE_NOOP_ALIAS,QT_TRANSLATE_NOOP_UTF8+=QT_TRANSLATE_NOOP_UTF8_ALIAS,QT_TRANSLATE_NOOP3+=QT_TRANSLATE_NOOP3_ALIAS,QT_TRANSLATE_NOOP3_UTF8+=QT_TRANSLATE_NOOP3_UTF8_ALIAS,QT_TRID_NOOP+=QT_TRID_NOOP_ALIAS,Q_DECLARE_TR_FUNCTIONS+=Q_DECLARE_TR_FUNCTIONS_ALIAS,qtTrId+=qtTrId_alias,QT_TR_NOOP+=QT_TR_NOOP_ALIAS,QT_TR_NOOP_UTF8+=QT_TR_NOOP_UTF8_ALIAS,tr+=tr_alias,trUtf8+=trUtf8_alias,translate+=translate_alias -ts project.ts
+lupdate main.cpp main.qml -tr-function-alias QT_TRANSLATE_NOOP+=QT_TRANSLATE_NOOP_ALIAS,QT_TRANSLATE_NOOP_UTF8+=QT_TRANSLATE_NOOP_UTF8_ALIAS,QT_TRANSLATE_NOOP3+=QT_TRANSLATE_NOOP3_ALIAS,QT_TRANSLATE_NOOP3_UTF8+=QT_TRANSLATE_NOOP3_UTF8_ALIAS,QT_TRID_NOOP+=QT_TRID_NOOP_ALIAS,Q_DECLARE_TR_FUNCTIONS+=Q_DECLARE_TR_FUNCTIONS_ALIAS,qtTrId+=qtTrId_alias,QT_TR_NOOP+=QT_TR_NOOP_ALIAS,QT_TR_NOOP_UTF8+=QT_TR_NOOP_UTF8_ALIAS,tr+=tr_alias,trUtf8+=trUtf8_alias,translate+=translate_alias,qsTr=MySingletonCustomTranslator.subObject.tr -ts project.ts
diff --git a/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/main.qml b/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/main.qml
new file mode 100644
index 000000000..3fd65ce9f
--- /dev/null
+++ b/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/main.qml
@@ -0,0 +1,4 @@
+import MyModuleWithSingleton 1.0
+Text {
+ text: MySingletonCustomTranslator.subObject.tr("xxx")
+}
diff --git a/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/project.ts.result
index 93e5b9781..974c4523b 100644
--- a/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/project.ts.result
+++ b/tests/auto/linguist/lupdate/testdata/good/tr_function_alias/project.ts.result
@@ -130,6 +130,14 @@
</message>
</context>
<context>
+ <name>main</name>
+ <message>
+ <location filename="main.qml" line="3"/>
+ <source>xxx</source>
+ <translation type="unfinished"></translation>
+ </message>
+</context>
+<context>
<name>scope</name>
<message>
<location filename="main.cpp" line="34"/>