1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <qtest.h>
30#include <QQmlEngine>
31#include <QQmlComponent>
32#include <QTranslator>
33#include <QQmlContext>
34#include <private/qqmlengine_p.h>
35#include <private/qqmltypedata_p.h>
36#include "../../shared/util.h"
37
38class tst_qqmltranslation : public QQmlDataTest
39{
40 Q_OBJECT
41public:
42 tst_qqmltranslation() {}
43
44private slots:
45 void translation_data();
46 void translation();
47 void idTranslation();
48 void translationChange();
49 void preferJSContext();
50};
51
52void tst_qqmltranslation::translation_data()
53{
54 QTest::addColumn<QString>(name: "translation");
55 QTest::addColumn<QUrl>(name: "testFile");
56 QTest::addColumn<bool>(name: "verifyCompiledData");
57
58 QTest::newRow(dataTag: "qml") << QStringLiteral("qml_fr") << testFileUrl(fileName: "translation.qml") << true;
59 QTest::newRow(dataTag: "qrc") << QStringLiteral(":/qml_fr.qm") << QUrl("qrc:/translation.qml") << true;
60 QTest::newRow(dataTag: "js") << QStringLiteral("qml_fr") << testFileUrl(fileName: "jstranslation.qml") << false;
61}
62
63void tst_qqmltranslation::translation()
64{
65 QFETCH(QString, translation);
66 QFETCH(QUrl, testFile);
67 QFETCH(bool, verifyCompiledData);
68
69 QTranslator translator;
70 translator.load(filename: translation, directory: dataDirectory());
71 QCoreApplication::installTranslator(messageFile: &translator);
72
73 QQmlEngine engine;
74 QQmlComponent component(&engine, testFile);
75 QObject *object = component.create();
76 QVERIFY(object != nullptr);
77
78 if (verifyCompiledData) {
79 QQmlContext *context = qmlContext(object);
80 QQmlEnginePrivate *engine = QQmlEnginePrivate::get(e: context->engine());
81 QQmlRefPointer<QQmlTypeData> typeData = engine->typeLoader.getType(unNormalizedUrl: context->baseUrl());
82 QV4::CompiledData::CompilationUnit *compilationUnit = typeData->compilationUnit();
83 QVERIFY(compilationUnit);
84
85 QSet<QString> compiledTranslations;
86 compiledTranslations << QStringLiteral("basic")
87 << QStringLiteral("disambiguation")
88 << QStringLiteral("singular") << QStringLiteral("plural");
89
90 const QV4::CompiledData::Object *rootObject
91 = compilationUnit->qmlData->objectAt(/*root object*/idx: 0);
92 const QV4::CompiledData::Binding *binding = rootObject->bindingTable();
93 for (quint32 i = 0; i < rootObject->nBindings; ++i, ++binding) {
94 const QString propertyName = compilationUnit->stringAt(index: binding->propertyNameIndex);
95
96 const bool expectCompiledTranslation = compiledTranslations.contains(value: propertyName);
97
98 if (expectCompiledTranslation) {
99 if (binding->type != QV4::CompiledData::Binding::Type_Translation)
100 qDebug() << "binding for property" << propertyName << "is not a compiled translation";
101 QCOMPARE(quint32(binding->type), quint32(QV4::CompiledData::Binding::Type_Translation));
102 } else {
103 if (binding->type == QV4::CompiledData::Binding::Type_Translation)
104 qDebug() << "binding for property" << propertyName << "is not supposed to be a compiled translation";
105 QVERIFY(binding->type != QV4::CompiledData::Binding::Type_Translation);
106 }
107 }
108 }
109
110 QCOMPARE(object->property("basic").toString(), QLatin1String("bonjour"));
111 QCOMPARE(object->property("basic2").toString(), QLatin1String("au revoir"));
112 QCOMPARE(object->property("basic3").toString(), QLatin1String("bonjour"));
113 QCOMPARE(object->property("disambiguation").toString(), QLatin1String("salut"));
114 QCOMPARE(object->property("disambiguation2").toString(), QString::fromUtf8("\xc3\xa0 plus tard"));
115 QCOMPARE(object->property("disambiguation3").toString(), QLatin1String("salut"));
116 QCOMPARE(object->property("noop").toString(), QLatin1String("bonjour"));
117 QCOMPARE(object->property("noop2").toString(), QLatin1String("au revoir"));
118 QCOMPARE(object->property("singular").toString(), QLatin1String("1 canard"));
119 QCOMPARE(object->property("singular2").toString(), QLatin1String("1 canard"));
120 QCOMPARE(object->property("plural").toString(), QLatin1String("2 canards"));
121 QCOMPARE(object->property("plural2").toString(), QLatin1String("2 canards"));
122
123 QCoreApplication::removeTranslator(messageFile: &translator);
124 delete object;
125}
126
127void tst_qqmltranslation::idTranslation()
128{
129 QTranslator translator;
130 translator.load(filename: QLatin1String("qmlid_fr"), directory: dataDirectory());
131 QCoreApplication::installTranslator(messageFile: &translator);
132
133 QQmlEngine engine;
134 QQmlComponent component(&engine, testFileUrl(fileName: "idtranslation.qml"));
135 QObject *object = component.create();
136 QVERIFY(object != nullptr);
137
138 {
139 QQmlContext *context = qmlContext(object);
140 QQmlEnginePrivate *engine = QQmlEnginePrivate::get(e: context->engine());
141 QQmlRefPointer<QQmlTypeData> typeData = engine->typeLoader.getType(unNormalizedUrl: context->baseUrl());
142 QV4::CompiledData::CompilationUnit *compilationUnit = typeData->compilationUnit();
143 QVERIFY(compilationUnit);
144
145 const QV4::CompiledData::Object *rootObject
146 = compilationUnit->qmlData->objectAt(/*root object*/idx: 0);
147 const QV4::CompiledData::Binding *binding = rootObject->bindingTable();
148 for (quint32 i = 0; i < rootObject->nBindings; ++i, ++binding) {
149 const QString propertyName = compilationUnit->stringAt(index: binding->propertyNameIndex);
150 if (propertyName == "idTranslation") {
151 if (binding->type != QV4::CompiledData::Binding::Type_TranslationById)
152 qDebug() << "binding for property" << propertyName << "is not a compiled translation";
153 QCOMPARE(quint32(binding->type), quint32(QV4::CompiledData::Binding::Type_TranslationById));
154 } else {
155 QVERIFY(binding->type != QV4::CompiledData::Binding::Type_Translation);
156 }
157 }
158 }
159
160 QCOMPARE(object->property("idTranslation").toString(), QLatin1String("bonjour tout le monde"));
161 QCOMPARE(object->property("idTranslation2").toString(), QLatin1String("bonjour tout le monde"));
162 QCOMPARE(object->property("idTranslation3").toString(), QLatin1String("bonjour tout le monde"));
163
164 QCoreApplication::removeTranslator(messageFile: &translator);
165 delete object;
166}
167
168class DummyTranslator : public QTranslator
169{
170 Q_OBJECT
171
172 QString translate(const char *context, const char *sourceText, const char *disambiguation, int n) const override
173 {
174 Q_UNUSED(context);
175 Q_UNUSED(disambiguation);
176 Q_UNUSED(n);
177 if (!qstrcmp(str1: sourceText, str2: "translate me"))
178 return QString::fromUtf8(str: "xxx");
179 if (!qstrcmp(str1: sourceText, str2: "English in mylibrary") && !qstrcmp(str1: context, str2: "mylibrary"))
180 return QString::fromUtf8(str: "Deutsch in mylibrary");
181 if (!qstrcmp(str1: sourceText, str2: "English in translation") && !qstrcmp(str1: context, str2: "nested_js_translation"))
182 return QString::fromUtf8(str: "Deutsch in Setzung");
183 return QString();
184 }
185
186 bool isEmpty() const override
187 {
188 return false;
189 }
190};
191
192void tst_qqmltranslation::translationChange()
193{
194 QQmlEngine engine;
195
196 QQmlComponent component(&engine, testFileUrl(fileName: "translationChange.qml"));
197 QScopedPointer<QObject> object(component.create());
198 QVERIFY(!object.isNull());
199
200 QCOMPARE(object->property("baseProperty").toString(), QString::fromUtf8("do not translate"));
201 QCOMPARE(object->property("text1").toString(), QString::fromUtf8("translate me"));
202 QCOMPARE(object->property("text2").toString(), QString::fromUtf8("translate me"));
203 QCOMPARE(object->property("text3").toString(), QString::fromUtf8("translate me"));
204 QCOMPARE(object->property("fromListModel").toString(), QString::fromUtf8("translate me"));
205
206 DummyTranslator translator;
207 QCoreApplication::installTranslator(messageFile: &translator);
208
209 QEvent ev(QEvent::LanguageChange);
210 QCoreApplication::sendEvent(receiver: &engine, event: &ev);
211
212 QCOMPARE(object->property("baseProperty").toString(), QString::fromUtf8("do not translate"));
213 QCOMPARE(object->property("text1").toString(), QString::fromUtf8("xxx"));
214 QCOMPARE(object->property("text2").toString(), QString::fromUtf8("xxx"));
215 QCOMPARE(object->property("text3").toString(), QString::fromUtf8("xxx"));
216 QCOMPARE(object->property("fromListModel").toString(), QString::fromUtf8("xxx"));
217
218 QCoreApplication::removeTranslator(messageFile: &translator);
219}
220
221void tst_qqmltranslation::preferJSContext()
222{
223 DummyTranslator translator;
224 QCoreApplication::installTranslator(messageFile: &translator);
225
226 QQmlEngine engine;
227 QQmlComponent component(&engine, testFileUrl(fileName: "preferjs.qml"));
228 QScopedPointer<QObject> object(component.create());
229 QVERIFY(!object.isNull());
230
231 QCOMPARE(object->property("german1").toString(),
232 QStringLiteral("Deutsch in Setzung"));
233 QCOMPARE(object->property("german2").toString(),
234 QStringLiteral("Deutsch in mylibrary"));
235
236 QCoreApplication::removeTranslator(messageFile: &translator);
237}
238
239QTEST_MAIN(tst_qqmltranslation)
240
241#include "tst_qqmltranslation.moc"
242

source code of qtdeclarative/tests/auto/qml/qqmltranslation/tst_qqmltranslation.cpp