summaryrefslogtreecommitdiffstats
path: root/tests/auto/cmake/test_translation_api/CMakeLists.txt
blob: 9d139456667e3d1a7edf1e164f1eb83d39db7d53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
cmake_minimum_required(VERSION 3.16)

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()

function(expect_files_in_list list_var)
    set(file_names "")
    foreach(path IN LISTS "${list_var}")
        get_filename_component(file_name "${path}" NAME)
        list(APPEND file_names "${file_name}")
    endforeach()
    set(found_file_names "")
    foreach(expected_file_name IN LISTS ARGN)
        list(FIND file_names "${expected_file_name}" idx)
        if(idx EQUAL -1)
            message(FATAL_ERROR "Expected file name '${expected_file_name}' is not in "
                "'${list_var}'. Its value is '${${list_var}}'.")
        endif()
        list(APPEND found_file_names "${expected_file_name}")
    endforeach()
    list(REMOVE_ITEM file_names ${found_file_names})
    list(LENGTH file_names n)
    if(NOT n EQUAL 0)
        message(FATAL_ERROR "Unexpected file names in '${list_var}': ${file_names}\n"
            "Value of '${list_var}' is '${${list_var}}'.")
    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
    QM_FILES_OUTPUT_VARIABLE qm_files)
not_expect_target(app2_lupdate)
expect_target(app2_lrelease)
not_expect_target(update_translations)
not_expect_target(release_translations)
expect_files_in_list(qm_files myobject_de.qm myobject_en.qm)

# 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
    QM_FILES_OUTPUT_VARIABLE qm_files2)
expect_target(app3_lupdate)
expect_target(app3_lrelease)
expect_target(release_translations)
expect_files_in_list(qm_files2 myobject_no.qm myobject_fi.qm)

# Now do the same with qt6_add_translations.
create_app(app4)
qt6_add_translations(app4
    TS_FILES myobject_no.ts myobject_fi.ts
    QM_FILES_OUTPUT_VARIABLE qm_files
    LUPDATE_OPTIONS -source-language en_US
    LRELEASE_OPTIONS -compress)
expect_target(app4_lupdate)
expect_target(app4_lrelease)
expect_target(release_translations)
expect_files_in_list(qm_files myobject_no.qm myobject_fi.qm)

# Typical usage of qt_add_translations with a generated resource.
create_app(app5)
qt6_add_translations(app5
    TS_FILES myobject_ru.ts
    RESOURCE_PREFIX "/tränslehschns")

# qt_add_translations on a static lib with a generated resource and the default resource prefix.
# Extract the created resource targets.
add_library(staticlib1 STATIC
    myi18nobject.cpp)
target_include_directories(staticlib1
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/some_dir)
target_link_libraries(staticlib1 PRIVATE Qt6::Core)
set(staticlib1_resource_targets "")
qt6_add_translations(staticlib1
    TS_FILES myobject_da.ts
    OUTPUT_TARGETS staticlib1_resource_targets)
if("${staticlib1_resource_targets}" STREQUAL "")
    message(FATAL_ERROR "staticlib1_resource_targets is empty.")
endif()

# Build the update_translations target
add_custom_target(my_all_target
    ALL
    DEPENDS update_translations)