summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJoerg Bornemann <joerg.bornemann@qt.io>2021-05-27 17:59:17 +0200
committerJoerg Bornemann <joerg.bornemann@qt.io>2021-06-03 20:26:14 +0200
commit22c5a32c096997266d627677cf8e281c186304d7 (patch)
tree0632dc48a942af2d3ef9f91e58c6e1792b7a7b15 /tests
parent00a1cc95520b5f6cca37e55a5d0a75a79b5b42b3 (diff)
CMake: Introduce new lupdate/lrelease API
Add a new CMake API for lupdate and lrelease which is target-based and aims to replace qt6_add_translation and qt6_create_translation. qt6_add_lupdate(my_app TS_FILES app_de.ts) does the following: - retrieves the sources from target my_app - adds the target my_app_lupdate to create/update .ts files - add the target update_translations to build all such *_lupdate targets qt6_add_lrelease(my_app TS_FILES app_en.ts) does the following: - adds the target my_app_lrelease to create .qm files - adds my_app_lrelease as dependency to my_app such that .qm files are automatically updated. - add the target release_translations to build all such *_lrelease targets qt6_add_translations(my_app TS_FILES app_en.ts) does all of the above with one call. The creation of the update_translations and release_translations targets can be prevented. Fixes: QTBUG-76410 Change-Id: Ibc8ed66e6cd3875447f4cf07d944227678997107 Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/cmake/CMakeLists.txt6
-rw-r--r--tests/auto/cmake/test_translation_api/CMakeLists.txt74
-rw-r--r--tests/auto/cmake/test_translation_api/myi18nobject.cpp49
-rw-r--r--tests/auto/cmake/test_translation_api/myobject_de.ts12
-rw-r--r--tests/auto/cmake/test_translation_api/some_dir/some_include.h37
5 files changed, 177 insertions, 1 deletions
diff --git a/tests/auto/cmake/CMakeLists.txt b/tests/auto/cmake/CMakeLists.txt
index d7e981e71..77641e8b0 100644
--- a/tests/auto/cmake/CMakeLists.txt
+++ b/tests/auto/cmake/CMakeLists.txt
@@ -28,7 +28,7 @@ project(qttools_cmake_tests)
enable_testing()
set(required_packages Core)
-set(optional_packages Widgets)
+set(optional_packages Widgets LinguistTools)
# Setup the test when called as a completely standalone project.
if(TARGET Qt6::Core)
@@ -61,3 +61,7 @@ if (TARGET Qt6::Widgets)
_qt_internal_test_expect_pass(test_uiplugin_module)
_qt_internal_test_expect_pass(test_uiplugin_via_designer)
endif()
+
+if(TARGET Qt6::lupdate)
+ _qt_internal_test_expect_pass(test_translation_api)
+endif()
diff --git a/tests/auto/cmake/test_translation_api/CMakeLists.txt b/tests/auto/cmake/test_translation_api/CMakeLists.txt
new file mode 100644
index 000000000..8bd69f9fe
--- /dev/null
+++ b/tests/auto/cmake/test_translation_api/CMakeLists.txt
@@ -0,0 +1,74 @@
+cmake_minimum_required(VERSION 3.15)
+
+project(test_update_translation_macro)
+
+find_package(Qt6 REQUIRED COMPONENTS Core LinguistTools)
+
+function(create_app target)
+ add_executable(${target}
+ myi18nobject.cpp)
+
+ target_include_directories(${target}
+ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/some_dir)
+
+ target_link_libraries(${target} Qt6::Core)
+endfunction()
+
+function(expect_target target)
+ if(NOT TARGET ${target})
+ message(FATAL_ERROR "Expected target '${target}' does not exist.")
+ endif()
+endfunction()
+
+function(not_expect_target target)
+ if(TARGET ${target})
+ message(FATAL_ERROR "Target '${target}' exists unexpectedly.")
+ endif()
+endfunction()
+
+# Test NO_GLOBAL_TARGET for lupdate.
+create_app(app1)
+qt6_add_lupdate(app1
+ NO_GLOBAL_TARGET
+ TS_FILES myobject_de.ts)
+expect_target(app1_lupdate)
+not_expect_target(app1_lrelease)
+not_expect_target(update_translations)
+not_expect_target(release_translations)
+
+# Test NO_GLOBAL_TARGET for lrelease.
+create_app(app2)
+qt6_add_lrelease(app2
+ NO_GLOBAL_TARGET
+ TS_FILES myobject_de.ts myobject_en.ts)
+not_expect_target(app2_lupdate)
+expect_target(app2_lrelease)
+not_expect_target(update_translations)
+not_expect_target(release_translations)
+
+# Typical usage of qt_add_lupdate/qt_add_lrelease. Pass some options for good measure.
+create_app(app3)
+qt6_add_lupdate(app3
+ TS_FILES myobject_no.ts myobject_fi.ts
+ OPTIONS -source-language en_US)
+qt6_add_lrelease(app3
+ TS_FILES myobject_no.ts myobject_fi.ts
+ OPTIONS -compress)
+expect_target(app3_lupdate)
+expect_target(app3_lrelease)
+expect_target(release_translations)
+
+# Now do the same with qt6_add_translations.
+create_app(app4)
+qt6_add_translations(app4
+ TS_FILES myobject_no.ts myobject_fi.ts
+ LUPDATE_OPTIONS -source-language en_US
+ LRELEASE_OPTIONS -compress)
+expect_target(app4_lupdate)
+expect_target(app4_lrelease)
+expect_target(release_translations)
+
+# Build the update_translations target
+add_custom_target(my_all_target
+ ALL
+ DEPENDS update_translations)
diff --git a/tests/auto/cmake/test_translation_api/myi18nobject.cpp b/tests/auto/cmake/test_translation_api/myi18nobject.cpp
new file mode 100644
index 000000000..eacce70db
--- /dev/null
+++ b/tests/auto/cmake/test_translation_api/myi18nobject.cpp
@@ -0,0 +1,49 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QDebug>
+#include <QCoreApplication>
+#include <QLocale>
+#include <QTranslator>
+
+#include "some_include.h"
+
+int main(int argc, char **argv)
+{
+ QCoreApplication app(argc, argv);
+ QTranslator *myappTranslator = new QTranslator;
+ QString localeName = QLocale::system().name();
+ if (!myappTranslator->load("myobject_" + localeName + ".qm", qApp->applicationDirPath()))
+ return 1;
+ myappTranslator->setObjectName("myobject_" + localeName);
+ app.installTranslator(myappTranslator);
+
+ qDebug() << QObject::tr("Hello, world!");
+ return 0;
+}
diff --git a/tests/auto/cmake/test_translation_api/myobject_de.ts b/tests/auto/cmake/test_translation_api/myobject_de.ts
new file mode 100644
index 000000000..fb4ee6f55
--- /dev/null
+++ b/tests/auto/cmake/test_translation_api/myobject_de.ts
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="de_DE">
+<context>
+ <name>QObject</name>
+ <message>
+ <location filename="myi18nobject.cpp" line="47"/>
+ <source>Hello, world!</source>
+ <translation type="unfinished"></translation>
+ </message>
+</context>
+</TS>
diff --git a/tests/auto/cmake/test_translation_api/some_dir/some_include.h b/tests/auto/cmake/test_translation_api/some_dir/some_include.h
new file mode 100644
index 000000000..627b9e9e3
--- /dev/null
+++ b/tests/auto/cmake/test_translation_api/some_dir/some_include.h
@@ -0,0 +1,37 @@
+/****************************************************************************
+**
+** Copyright (C) 2021 The Qt Company Ltd.
+** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef SOME_INCLUDE_H
+#define SOME_INCLUDE_H
+
+enum {
+ Non_Empty_File
+};
+
+#endif