aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qmltc
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2022-08-02 16:20:07 +0200
committerSami Shalayel <sami.shalayel@qt.io>2022-08-25 11:35:57 +0200
commit75769767ace0f16cc1be2e41fbd239b202394c7a (patch)
tree9d16fe8a793f5d586e1f0e2c7e7efeb2ba2d0398 /tests/auto/qml/qmltc
parente3aeb13052c1d783c92a6043614a196a267d5fea (diff)
qmltc: translation binding support
Implement and test support for translation bindings in qmltc: * qsTr() * QT_TR_NOOP() * qsTrId() * QT_TRID_NOOP() Not compiled by qmltc, but instead interpreted as script bindings: * combinations like qsTr(qsTr()) * qsTranslate() (as in qmlsc) * QT_TRANSLATE_NOOP() (as in qmlsc) Add the *.qm files directly to the resources as qt_add_translations() is not available from qtdeclarative (the cmake function lives in qttools that depends on qtdeclarative). Fixes: QTBUG-104637 Task-Id: QTBUG-105346 Change-Id: Ia9433c2bcef01f3486358d963059d9779c67708c Reviewed-by: Sami Shalayel <sami.shalayel@qt.io> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/qml/qmltc')
-rw-r--r--tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt16
-rw-r--r--tests/auto/qml/qmltc/QmltcTests/translations.qml21
-rw-r--r--tests/auto/qml/qmltc/QmltcTests/translationsById.qml16
-rw-r--r--tests/auto/qml/qmltc/QmltcTests/translationsById_ge.qmbin0 -> 200 bytes
-rw-r--r--tests/auto/qml/qmltc/QmltcTests/translationsById_ge.ts22
-rw-r--r--tests/auto/qml/qmltc/QmltcTests/translations_ge.qmbin0 -> 730 bytes
-rw-r--r--tests/auto/qml/qmltc/QmltcTests/translations_ge.ts65
-rw-r--r--tests/auto/qml/qmltc/tst_qmltc.cpp66
-rw-r--r--tests/auto/qml/qmltc/tst_qmltc.h1
9 files changed, 207 insertions, 0 deletions
diff --git a/tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt b/tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt
index 89159918be..7a536c92be 100644
--- a/tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt
+++ b/tests/auto/qml/qmltc/QmltcTests/CMakeLists.txt
@@ -94,6 +94,8 @@ set(qml_sources
deferredProperties_attached.qml
deferredProperties_complex.qml
valueTypeListProperty.qml
+ translations.qml
+ translationsById.qml
# support types:
DefaultPropertySingleChild.qml
@@ -133,6 +135,18 @@ qt_autogen_tools_initial_setup(qmltc_test_module)
target_include_directories(qmltc_test_module PUBLIC cpptypes/)
target_link_libraries(qmltc_test_module PUBLIC ${common_libraries})
+# Resources:
+set(qmake_immediate_resource_files
+ "translations_ge.qm"
+ "translationsById_ge.qm"
+)
+qt_internal_add_resource(qmltc_test_module "qmake_immediate"
+ PREFIX
+ "/i18n"
+ FILES
+ ${qmake_immediate_resource_files}
+)
+
qt6_add_qml_module(qmltc_test_module
VERSION 1.0
URI QmltcTests
@@ -144,6 +158,8 @@ qt6_add_qml_module(qmltc_test_module
${js_sources}
DEPENDENCIES
QtQuick
+ LIBRARIES
+ qmltc_test_module_translation_by_id
ENABLE_TYPE_COMPILER
TYPE_COMPILER_NAMESPACE QmltcTest
)
diff --git a/tests/auto/qml/qmltc/QmltcTests/translations.qml b/tests/auto/qml/qmltc/QmltcTests/translations.qml
new file mode 100644
index 0000000000..c7bf9553f9
--- /dev/null
+++ b/tests/auto/qml/qmltc/QmltcTests/translations.qml
@@ -0,0 +1,21 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+import QtQuick
+
+Text {
+ property string alsoTranslated
+ alsoTranslated: qsTr("Bye bye!", "") // has bindable, compiled as translation by qmltc
+ text: qsTr("Bye bye!") // has no bindable, compiled as translation by qmltc
+
+ property string hardcodedContext: qsTranslate("translations", "Bye bye!", "") //compiled as Script
+ property string anotherContext: qsTranslate("anotherContext", "Bye bye!", "") //compiled as Script
+ property string toBeTranslatedLater: QT_TR_NOOP("Bye bye!") //compiled as String Literal
+ property string toBeTranslatedLaterWithHardcodedContext: QT_TRANSLATE_NOOP("translations", "Bye bye!", "", -1) //compiled as Script
+ property string translatedN: qsTr("The solution is %n", "", 42) //compiled as translation by qmltc
+
+ property string translatedNWithContextAndAmbiguation: qsTranslate("translations", "The solution has %n degrees", "solution to problem" , 42) //compiled as script
+ property string translatedNWithContextAndAmbiguation2: qsTranslate("translations", "The solution has %n degrees", "chemical solution" , 43) //compiled as script
+
+ property string combination: qsTr(qsTr("Bye bye!", ""))
+}
diff --git a/tests/auto/qml/qmltc/QmltcTests/translationsById.qml b/tests/auto/qml/qmltc/QmltcTests/translationsById.qml
new file mode 100644
index 0000000000..e3606d9b0f
--- /dev/null
+++ b/tests/auto/qml/qmltc/QmltcTests/translationsById.qml
@@ -0,0 +1,16 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+import QtQuick
+
+// note: mixing translations with Ids with translations with texts is unsupported. Therefore this is separated from the translations.qml file.
+
+Text {
+ property string alsoTranslated
+ alsoTranslated: qsTrId("ID1") // has bindable, compiled as translation by qmltc
+ text: qsTrId("ID1") // has no bindable, compiled as translation by qmltc
+
+ property string toBeTranslatedLater: QT_TRID_NOOP("ID1") //compiled as String Literal by qmltc!
+
+ property string translatedN: qsTrId("ID2", 5) // compiled as translation by qmltc
+}
diff --git a/tests/auto/qml/qmltc/QmltcTests/translationsById_ge.qm b/tests/auto/qml/qmltc/QmltcTests/translationsById_ge.qm
new file mode 100644
index 0000000000..3c5fd16b00
--- /dev/null
+++ b/tests/auto/qml/qmltc/QmltcTests/translationsById_ge.qm
Binary files differ
diff --git a/tests/auto/qml/qmltc/QmltcTests/translationsById_ge.ts b/tests/auto/qml/qmltc/QmltcTests/translationsById_ge.ts
new file mode 100644
index 0000000000..2f91f0020a
--- /dev/null
+++ b/tests/auto/qml/qmltc/QmltcTests/translationsById_ge.ts
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="de_DE">
+<context>
+ <name></name>
+ <message id="ID1">
+ <location filename="translationsById.qml" line="10"/>
+ <location filename="translationsById.qml" line="11"/>
+ <location filename="translationsById.qml" line="13"/>
+ <source></source>
+ <translation>Tschüssi!</translation>
+ </message>
+ <message id="ID2" numerus="yes">
+ <location filename="translationsById.qml" line="15"/>
+ <source></source>
+ <translation>
+ <numerusform>Ich sehe %n Apfel</numerusform>
+ <numerusform>Ich sehe %n Äpfeln</numerusform>
+ </translation>
+ </message>
+</context>
+</TS>
diff --git a/tests/auto/qml/qmltc/QmltcTests/translations_ge.qm b/tests/auto/qml/qmltc/QmltcTests/translations_ge.qm
new file mode 100644
index 0000000000..fb57f73a6a
--- /dev/null
+++ b/tests/auto/qml/qmltc/QmltcTests/translations_ge.qm
Binary files differ
diff --git a/tests/auto/qml/qmltc/QmltcTests/translations_ge.ts b/tests/auto/qml/qmltc/QmltcTests/translations_ge.ts
new file mode 100644
index 0000000000..10cf7be332
--- /dev/null
+++ b/tests/auto/qml/qmltc/QmltcTests/translations_ge.ts
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="de_DE">
+<context>
+ <name>anotherContext</name>
+ <message>
+ <location filename="translations.qml" line="12"/>
+ <source>Bye bye!</source>
+ <translation>Bis später!</translation>
+ </message>
+</context>
+<context>
+ <name>notacontext</name>
+ <message numerus="yes">
+ <source>Bye bye!</source>
+ <translation type="obsolete">
+ <numerusform>Tschüssi!</numerusform>
+ <numerusform></numerusform>
+ </translation>
+ </message>
+</context>
+<context>
+ <name>translations</name>
+ <message>
+ <location filename="translations.qml" line="8"/>
+ <location filename="translations.qml" line="9"/>
+ <location filename="translations.qml" line="11"/>
+ <location filename="translations.qml" line="13"/>
+ <location filename="translations.qml" line="14"/>
+ <location filename="translations.qml" line="20"/>
+ <source>Bye bye!</source>
+ <translation>Tschüssi!</translation>
+ </message>
+ <message numerus="yes">
+ <location filename="translations.qml" line="15"/>
+ <source>The solution is %n</source>
+ <translation>
+ <numerusform>Die Lösung ist %n</numerusform>
+ <numerusform>Die Lösung ist %n</numerusform>
+ </translation>
+ </message>
+ <message numerus="yes">
+ <location filename="translations.qml" line="17"/>
+ <source>The solution has %n degrees</source>
+ <comment>solution to problem</comment>
+ <translation>
+ <numerusform>Die Lösung des Problems ist %n grad.</numerusform>
+ <numerusform>Die Lösung des Problems ist %n grad.</numerusform>
+ </translation>
+ </message>
+ <message numerus="yes">
+ <location filename="translations.qml" line="18"/>
+ <source>The solution has %n degrees</source>
+ <comment>chemical solution</comment>
+ <translation>
+ <numerusform>Die Lösung ist %n grad warm</numerusform>
+ <numerusform>Die Lösung ist %n grad warm</numerusform>
+ </translation>
+ </message>
+ <message>
+ <source>hello</source>
+ <translation type="vanished">hallo</translation>
+ </message>
+</context>
+</TS>
diff --git a/tests/auto/qml/qmltc/tst_qmltc.cpp b/tests/auto/qml/qmltc/tst_qmltc.cpp
index 9b6cd692b4..85cc82a1b1 100644
--- a/tests/auto/qml/qmltc/tst_qmltc.cpp
+++ b/tests/auto/qml/qmltc/tst_qmltc.cpp
@@ -70,6 +70,8 @@
#include "calqlatrbits.h"
#include "propertychangeandsignalhandlers.h"
#include "valuetypelistproperty.h"
+#include "translations.h"
+#include "translationsbyid.h"
#include "testprivateproperty.h"
@@ -87,6 +89,7 @@
#include <QtCore/private/qobject_p.h>
#include <QtTest/private/qemulationdetector_p.h>
+#include <QtCore/qtranslator.h>
#ifndef QMLTC_TESTS_DISABLE_CACHE
# error "QMLTC_TESTS_DISABLE_CACHE is supposed to be defined and be equal to either 0 or 1"
@@ -2239,4 +2242,67 @@ void tst_qmltc::valueTypeListProperty()
QVERIFY(!arrayOfColors2.isValid());
}
+void tst_qmltc::translations()
+{
+ {
+ QQmlEngine e;
+ PREPEND_NAMESPACE(translations) created(&e);
+
+ QCOMPARE(created.alsoTranslated(), "Bye bye!");
+ QCOMPARE(created.text(), "Bye bye!");
+
+ QCOMPARE(created.hardcodedContext(), "Bye bye!");
+ QCOMPARE(created.anotherContext(), "Bye bye!");
+ QCOMPARE(created.toBeTranslatedLater(), "Bye bye!");
+ QCOMPARE(created.toBeTranslatedLaterWithHardcodedContext(), "Bye bye!");
+
+ QCOMPARE(created.translatedN(), "The solution is 42");
+ QCOMPARE(created.translatedNWithContextAndAmbiguation(), "The solution has 42 degrees");
+ QCOMPARE(created.translatedNWithContextAndAmbiguation2(), "The solution has 43 degrees");
+
+ QCOMPARE(created.combination(), "Bye bye!");
+
+ QTranslator translator;
+ QVERIFY(translator.load("translations_ge.qm", ":/i18n"));
+ QVERIFY(qApp->installTranslator(&translator));
+ e.retranslate();
+
+ QCOMPARE(created.text(), u"Tschüssi!"_s);
+ QCOMPARE(created.alsoTranslated(), u"Tschüssi!"_s);
+
+ QCOMPARE(created.hardcodedContext(), u"Tschüssi!"_s);
+ QCOMPARE(created.anotherContext(), u"Bis später!"_s);
+ QCOMPARE(created.toBeTranslatedLater(), "Bye bye!");
+ QCOMPARE(created.toBeTranslatedLaterWithHardcodedContext(), "Bye bye!");
+
+ QCOMPARE(created.translatedN(), u"Die Lösung ist 42"_s);
+ QCOMPARE(created.translatedNWithContextAndAmbiguation(),
+ u"Die Lösung des Problems ist 42 grad."_s);
+ QCOMPARE(created.translatedNWithContextAndAmbiguation2(), u"Die Lösung ist 43 grad warm"_s);
+
+ QCOMPARE(created.combination(), u"Tschüssi!"_s);
+ }
+ {
+ QQmlEngine e;
+ PREPEND_NAMESPACE(translationsById) created(&e);
+
+ QCOMPARE(created.alsoTranslated(), "ID1");
+ QCOMPARE(created.text(), "ID1");
+
+ QCOMPARE(created.toBeTranslatedLater(), "ID1");
+ QCOMPARE(created.translatedN(), "ID2");
+
+ QTranslator translator;
+ QVERIFY(translator.load("translationsById_ge.qm", ":/i18n"));
+ QVERIFY(qApp->installTranslator(&translator));
+ e.retranslate();
+
+ QCOMPARE(created.text(), u"Tschüssi!"_s);
+ QCOMPARE(created.alsoTranslated(), u"Tschüssi!"_s);
+
+ QCOMPARE(created.toBeTranslatedLater(), "ID1");
+ QCOMPARE(created.translatedN(), u"Ich sehe 5 Äpfeln"_s);
+ }
+}
+
QTEST_MAIN(tst_qmltc)
diff --git a/tests/auto/qml/qmltc/tst_qmltc.h b/tests/auto/qml/qmltc/tst_qmltc.h
index 60209b9a47..f2c68d2e8d 100644
--- a/tests/auto/qml/qmltc/tst_qmltc.h
+++ b/tests/auto/qml/qmltc/tst_qmltc.h
@@ -83,4 +83,5 @@ private slots:
void calqlatrBits(); // corner cases from calqlatr demo
void trickyPropertyChangeAndSignalHandlers();
void valueTypeListProperty();
+ void translations();
};