Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cmake-macros.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\group cmake-commands-qtlinguisttools
6\title CMake Commands in Qt6 LinguistTools
7
8The following CMake commands are defined when Qt6::LinguistTools is loaded, for instance
9with
10
11\code
12find_package(Qt6 REQUIRED COMPONENTS LinguistTools)
13\endcode
14
15\sa{CMake Command Reference}
16*/
17
18/*!
19\page qtlinguist-cmake-qt-add-translation.html
20\ingroup cmake-commands-qtlinguisttools
21
22\title qt_add_translation
23\keyword qt6_add_translation
24
25\summary {Compiles Qt Linguist .ts files into .qm files.}
26
27\include cmake-find-package-linguisttools.qdocinc
28
29\warning This function is deprecated. Consider using the target-based
30functions \l{qt6_add_lrelease} or \l{qt6_add_translations} instead.
31
32\section1 Synopsis
33
34\badcode
35qt_add_translation(<VAR> file1.ts [file2.ts ...]
36 [OPTIONS ...])
37\endcode
38
39\versionlessCMakeCommandsNote qt6_add_translation
40
41\section1 Description
42
43Calls \c{lrelease} on each \c{.ts} file passed as an argument, generating
44\c{.qm} files. The paths of the generated files are added to \c{<VAR>}.
45
46\section1 Options
47
48You can set additional \c{OPTIONS} that should be passed when \c{lrelease} is
49invoked. You can find possible options in the \l{lrelease}{lrelease documentation}.
50
51By default, the \c{qm} files will be placed in the current build directory
52(\c{CMAKE_CURRENT_BINARY_DIR}). To change this, you can set \c{OUTPUT_LOCATION}
53as a property of the source \c{.ts} file before calling \c qt_add_translation.
54
55For example, with the following code, the \c{.qm} files are generated
56in a \c{translations} directory below the current build directory.
57
58\snippet cmake-macros/examples.cmake set_output_location_on_ts_file_with_qt_add_translation
59
60\section1 Examples
61
62Generating \c{helloworld_en.qm}, \c{helloworld_de.qm} in the build
63directory:
64
65\snippet cmake-macros/examples.cmake qt_add_translation
66
67Generating \c{helloworld_en.qm}, \c{helloworld_de.qm} in a \c{l10n}
68sub-directory:
69
70\snippet cmake-macros/examples.cmake qt_add_translation_output_location
71*/
72
73/*!
74\page qtlinguist-cmake-qt-create-translation.html
75\ingroup cmake-commands-qtlinguisttools
76
77\title qt_create_translation
78\keyword qt6_create_translation
79
80\summary {Sets up the Qt Linguist translation toolchain.}
81
82\include cmake-find-package-linguisttools.qdocinc
83
84\warning This function is deprecated. Consider using the target-based
85functions \l{qt6_add_lupdate} or \l{qt6_add_translations} instead.
86
87\section1 Synopsis
88
89\badcode
90qt_create_translation(<VAR> ts-file-or-sources [ts-file-or-sources2 ...]
91 [OPTIONS ...])
92\endcode
93
94\versionlessCMakeCommandsNote qt6_create_translation
95
96\section1 Description
97
98Processes given sources (directories or individual files) to generate
99Qt Linguist \c{.ts} files. The \c{.ts} files are in turn compiled into \c{.qm}
100files of the same base name that are stored in the build
101directory. Paths to the generated \c{.qm} files are added to \c{<VAR>}.
102
103The translation files to create or update need to have a \c{.ts} suffix. If
104the given file path is not absolute it is resolved relative to the current
105source directory. If no \c{.ts} file is passed as an argument, the macro
106does nothing.
107
108Any arguments that do not have a \c{.ts} suffix are passed as input to the
109\c{lupdate}. \c{lupdate} accepts directories and source files as input.
110See also the \l{lupdate}{lupdate documentation} on further details.
111
112\section1 Options
113
114You can set additional \c{OPTIONS} that should be passed when \c{lupdate} is
115invoked. You can find possible options in the \l{lupdate}{lupdate documentation}.
116
117\section1 Examples
118
119Recursively look up Qt translations from source files in current directory and
120generate or update \c{helloworld_en.ts} and \c{helloworld_de.ts} file using
121\c{lupdate}. Compile said files into \c{helloworld_en.qm} and \c{helloworld.de.qm}
122files in the build directory:
123
124\snippet cmake-macros/examples.cmake qt_create_translation
125*/
126
127/*!
128\page qtlinguist-cmake-qt-add-lupdate.html
129\ingroup cmake-commands-qtlinguisttools
130
131\title qt_add_lupdate
132\keyword qt6_add_lupdate
133
134\summary {Add targets to generate or update Qt Linguist .ts files.}
135
136\include cmake-find-package-linguisttools.qdocinc
137
138\cmakecommandsince 6.2
139
140\section1 Synopsis
141
142Since 6.7:
143\badcode
144qt_add_lupdate(TS_FILES file1.ts [file2.ts ...]
145 [PLURALS_TS_FILE file.ts]
146 [SOURCE_TARGETS target1 [target2 ...]]
147 [SOURCES source1.cpp [sources2.cpp ...]]
148 [INCLUDE_DIRECTORIES directory1 [directory2 ...]]
149 [LUPDATE_TARGET target-name]
150 [NO_GLOBAL_TARGET]
151 [OPTIONS ...])
152\endcode
153
154Since 6.2 (deprecated):
155\badcode
156qt_add_lupdate(target TS_FILES file1.ts [file2.ts ...]
157 [SOURCES source1.cpp [sources2.cpp ...]]
158 [INCLUDE_DIRECTORIES directory1 [directory2 ...]]
159 [NO_GLOBAL_TARGET]
160 [OPTIONS ...])
161\endcode
162
163\versionlessCMakeCommandsNote qt6_add_lupdate()
164
165\section1 Description
166
167Creates a custom target to generate or update Qt Linguist \c{.ts}
168files with \l{lupdate}.
169
170The name of that custom target defaults to \c{${PROJECT_NAME}_lupdate}. Further
171calls of \c qt_add_lupdate will create target names with an increasing number
172appended. The custom target name can be specified with the \c LUPDATE_TARGET
173option.
174
175With the \c{SOURCE_TARGETS} argument you can specify a list of targets that
176contain sources with translatable strings. If \c{SOURCE_TARGETS} is not
177specified, you can specify \c{SOURCES}, which is described below. If neither
178\c{SOURCE_TARGETS} nor \c{SOURCES} is given, the command
179\l{qt6_collect_translation_source_targets}{qt_collect_translation_source_targets}
180is called to retrieve the list of targets.
181
182The \c{.ts} files must be specified with the argument \c{TS_FILES}.
183
184This function is designed to be used in conjunction with
185\l{qt6_add_lrelease}{qt_add_lrelease}. See also the convenience wrapper
186\l{qt6_add_translations}{qt_add_translations}.
187
188//! [sources-and-include-dirs]
189\section1 Sources and Include Directories
190
191With \c{SOURCES} you can explicitly specify additional source files that contain
192translatable strings.
193
194You can use \c{INCLUDE_DIRECTORIES} to explicitly specify include directories
195for those source files.
196//! [sources-and-include-dirs]
197
198\section1 Options
199
200You can set additional \c{OPTIONS} that should be passed when \c{lupdate} is
201invoked. You can find possible options in the \l{lupdate}{lupdate
202documentation}.
203
204\section1 Umbrella Target
205
206In addition to the target \c{${target}_lupdate}, an umbrella target
207\c{update_translations} is created. This target will build all
208\c{${target}_lupdate} targets that were created with \c{qt_add_lupdate}.
209
210Pass \c{NO_GLOBAL_TARGET} to \c{qt_add_lupdate} to prevent this behavior.
211
212The name of this target can be overridden by setting the variable
213\c{QT_GLOBAL_LUPDATE_TARGET} before calling \c{qt_add_lupdate}.
214
215//! [plurals-ts-file]
216\section1 Plural Forms
217
218\l QT_I18N_SOURCE_LANGUAGE specifies the language in which the source code
219strings are written. For handling plural forms correctly, create an additional
220\c{.ts} file for that language that only contains translatable strings for
221plural forms. See \l{Handle Plural Forms} for details.
222
223With \c PLURALS_TS_FILE you can specify the \c{.ts} file for the source
224language. This file will only contain plural forms.
225//! [plurals-ts-file]
226
227\section1 Deprecated Command Signature
228
229Older versions of \c qt_add_lupdate took a target as the first argument. This is
230deprecated. Use the \c SOURCE_TARGETS argument instead.
231
232\section1 Examples
233
234Add the targets \c{myapp_lupdate} and \c{update_translations} for updating the
235\c{.ts} file of an application \c{myapp}.
236
237\snippet cmake-macros/examples.cmake qt_add_lupdate
238
239You can specify the name of the created target by passing the \c LUPDATE_TARGET
240argument:
241
242\badcode
243qt_add_lupdate(
244 LUPDATE_TARGET update_application_translations
245 TS_FILES myapp_de.ts
246 PLURALS_TS_FILE myapp_en.ts
247)
248\endcode
249*/
250
251/*!
252\page qtlinguist-cmake-qt-add-lrelease.html
253\ingroup cmake-commands-qtlinguisttools
254
255\title qt_add_lrelease
256\keyword qt6_add_lrelease
257
258\summary {Add targets to transform Qt Linguist .ts files into .qm files.}
259
260\include cmake-find-package-linguisttools.qdocinc
261
262\cmakecommandsince 6.2
263
264\section1 Synopsis
265
266Since 6.7:
267\badcode
268qt_add_lrelease(TS_FILES file1.ts [file2.ts ...]
269 [LRELEASE_TARGET target-name]
270 [EXCLUDE_FROM_ALL]
271 [NO_GLOBAL_TARGET]
272 [QM_OUTPUT_DIRECTORY directory] # since 6.9
273 [QM_FILES_OUTPUT_VARIABLE variable-name]
274 [MERGE_QT_TRANSLATIONS]
275 [QT_TRANSLATION_CATALOGS catalog1 [catalog2 ...]]
276 [OPTIONS ...])
277\endcode
278
279Since 6.2 (deprecated):
280\badcode
281qt_add_lrelease(target TS_FILES file1.ts [file2.ts ...]
282 [NO_TARGET_DEPENDENCY]
283 [NO_GLOBAL_TARGET]
284 [QM_FILES_OUTPUT_VARIABLE variable-name]
285 [OPTIONS ...])
286\endcode
287
288\versionlessCMakeCommandsNote qt6_add_lrelease()
289
290\warning Calling \c qt_add_lrelease in a directory scope different than the
291target directory scope requires at least CMake version 3.18.
292
293\section1 Description
294
295Creates a custom command to transform \c{.ts} files into \c{.qm} files with
296\l{lrelease}.
297
298The execution of the custom command is driven by a custom target that is built
299by default. The name of that custom target defaults to
300\c{${PROJECT_NAME}_lrelease}. Further calls of \c qt_add_lrelease will create
301target names with an increasing number appended. The custom target name can be
302specified with the \c LRELEASE_TARGET option.
303
304The \c{.ts} files must be specified with the argument \c{TS_FILES}.
305
306This function is designed to be used in conjunction with
307\l{qt6_add_lupdate}{qt_add_lupdate}. See also the convenience wrapper
308\l{qt6_add_translations}{qt_add_translations}.
309
310\section1 Options
311
312You can set additional \c{OPTIONS} that should be passed when \c{lrelease} is
313invoked. You can find possible options in the \l{lrelease}{lrelease
314documentation}.
315
316\section1 Location of generated .qm files
317
318By default, the \c{.qm} files will be placed in the current build directory
319(\c{CMAKE_CURRENT_BINARY_DIR}). Since Qt 6.9 you can use the \c
320QM_OUTPUT_DIRECTORY argument to generate the \c{.qm} files in a different
321directory. Relative paths are considered to be below \c
322CMAKE_CURRENT_BINARY_DIR.
323
324For example, with the following code, the \c{.qm} files are generated
325in a \c{translations} directory below the current build directory.
326
327\snippet cmake-macros/examples.cmake set_qm_output_directory_with_qt_add_lrelease
328
329For more fine-grained control, you can specify the output directory of
330individual \c{.qm} files by setting the source file property \c{OUTPUT_LOCATION}
331on the corresponding \c{.ts} file. This must happen before calling \c
332qt_add_lrelease.
333
334The \c OUTPUT_LOCATION source file property overrides the directory passed via
335\c QM_OUTPUT_DIRECTORY.
336
337For example, with the following code, the \c{.qm} files are generated
338in a \c{translations} directory below the current build directory.
339
340\snippet cmake-macros/examples.cmake set_output_location_on_ts_file_with_qt_add_lrelease
341
342\section1 Processing Generated .qm Files
343
344To further process the generated \c{.qm} files, for example to create install
345rules, \c{qt_add_lrelease} can store the paths of the \c{.qm} files in a
346variable. Pass \c{QM_FILES_OUTPUT_VARIABLE <variable-name>} to the function for
347that.
348
349\section1 Build by Default
350
351By default, the command creates a custom target that is added to the default
352build target. This ensures that the \c{.qm} files are always up-to-date when the
353project is built. This behavior can be turned off with the \c{EXCLUDE_FROM_ALL}
354argument. In this case, the user must build the \c{${PROJECT_NAME}_lrelease}
355target manually.
356
357\section1 Umbrella Target
358
359In addition to the target \c{${target}_lrelease}, an umbrella target
360\c{release_translations} is created. This target will build all
361\c{${target}_lrelease} targets that were created with \c{qt_add_lrelease}.
362
363Pass \c{NO_GLOBAL_TARGET} to \c{qt_add_lrelease} to prevent this behavior.
364
365The name of this target can be overridden by setting the variable
366\c{QT_GLOBAL_LRELEASE_TARGET} before calling \c{qt_add_lrelease}.
367
368\section1 Merging Qt-provided translations
369
370Since Qt 6.9, you can merge the translations that are provided by Qt into
371application-specific \c{.qm} files. To do that, pass the \c
372MERGE_QT_TRANSLATIONS option to \c{qt_add_translations}. This will determine the
373Qt translation catalogs that belong to the modules used by your project and
374merge them into the \c{.qm} files that are produced by \c{lrelease}.
375
376The Qt translation catalogs are determined by the \c find_package calls in the
377scope of your \c{CMakeLists.txt} where \c qt_add_translations is called. For
378example, the following sequence will merge the catalogs \c qtbase and
379\c{qtmultimedia} into your \c{.qm} files:
380
381\badcode
382find_package(Qt6 COMPONENTS MultimediaWidgets)
383...
384qt_add_lrelease(
385 TS_FILES myapp_de.ts myapp_fi.ts
386 MERGE_QT_TRANSLATIONS
387)
388\endcode
389
390To explicitly specify the catalogs, use the \c QT_TRANSLATION_CATALOGS argument:
391
392\badcode
393qt_add_lrelease(
394 TS_FILES myapp_de.ts myapp_fi.ts
395 MERGE_QT_TRANSLATIONS
396 QT_TRANSLATION_CATALOGS qtbase qtmultimedia
397)
398\endcode
399
400If your project supports a language for which Qt doesn't offer its own
401translation, a warning will be issued at configure time. You can suppress this
402warning by setting the CMake variable \c QT_NO_MISSING_CATALOG_LANGUAGE_WARNING
403to \c{ON}.
404
405\section1 Deprecated Command Signature
406
407Older versions of \c qt_add_lrelease took a target as the first argument. This
408is still possible but deprecated.
409
410\section1 Examples
411
412Add the targets \c{myapp_lrelease} and \c{release_translations} for transforming
413the given \c{.ts} files into \c{.qm} files. Also, install the generated \c{.qm}
414files. The target \c{myapp_lrelease} is built by default.
415
416\badcode
417project(myapp)
418...
419qt_add_lrelease(
420 TS_FILES myapp_de.ts myapp_fr.ts
421 QM_FILES_OUTPUT_VARIABLE qm_files
422)
423install(FILES ${qm_files} DESTINATION "translations")
424\endcode
425
426You can specify the name of the created target by passing the \c LRELEASE_TARGET
427argument:
428
429\badcode
430qt_add_lrelease(
431 LRELEASE_TARGET create_myapp_qm_files
432 TS_FILES myapp_de.ts myapp_fr.ts
433 QM_FILES_OUTPUT_VARIABLE qm_files
434)
435\endcode
436*/
437
438/*!
439\page qtlinguist-cmake-qt-add-translations.html
440\ingroup cmake-commands-qtlinguisttools
441
442\title qt_add_translations
443\keyword qt6_add_translations
444
445\summary {Add targets to update and transform Qt Linguist .ts files into .qm files.}
446
447\include cmake-find-package-linguisttools.qdocinc
448
449\cmakecommandsince 6.2
450
451\section1 Synopsis
452
453Since Qt 6.7:
454\badcode
455qt_add_translations([target]
456 [TARGETS target1 [target2...]]
457 [SOURCE_TARGETS target1 [target2...]]
458 [TS_FILE_BASE name]
459 [TS_FILES file1.ts [file2.ts ...]]
460 [PLURALS_TS_FILE file.ts]
461 [NO_GENERATE_PLURALS_TS_FILE]
462 [RESOURCE_PREFIX prefix]
463 [OUTPUT_TARGETS variable-name]
464 [TS_FILES_OUTPUT_VARIABLE variable-name] # since 6.8
465 [TS_OUTPUT_DIRECTORY directory] # since 6.9
466 [QM_FILES_OUTPUT_VARIABLE variable-name]
467 [QM_OUTPUT_DIRECTORY directory] # since 6.9
468 [SOURCES source1.cpp [sources2.cpp ...]]
469 [INCLUDE_DIRECTORIES directory1 [directory2 ...]]
470 [LUPDATE_TARGET target-name]
471 [LUPDATE_OPTIONS ...]
472 [LRELEASE_TARGET target-name]
473 [LRELEASE_OPTIONS ...]
474 [MERGE_QT_TRANSLATIONS]
475 [QT_TRANSLATION_CATALOGS catalog1 [catalog2 ...]]
476 [IMMEDIATE_CALL])
477\endcode
478
479Since Qt 6.2 (deprecated):
480\badcode
481qt_add_translations(target TS_FILES file1.ts [file2.ts ...]
482 [RESOURCE_PREFIX prefix]
483 [OUTPUT_TARGETS variable-name]
484 [QM_FILES_OUTPUT_VARIABLE variable-name]
485 [SOURCES source1.cpp [sources2.cpp ...]]
486 [INCLUDE_DIRECTORIES directory1 [directory2 ...]]
487 [LUPDATE_OPTIONS ...]
488 [LRELEASE_OPTIONS ...])
489\endcode
490
491\versionlessCMakeCommandsNote qt6_add_translations()
492
493\warning Calling \c qt_add_translations in a directory scope different than the
494target directory scope requires at least CMake version 3.18.
495
496\section1 Description
497
498Creates targets for updating Qt Linguist \c{.ts} files and for transforming them
499into \c{.qm} files. This function is a convenience wrapper around
500\l{qt6_add_lupdate}{qt_add_lupdate} and \l{qt6_add_lrelease}{qt_add_lrelease}
501and aims to offer the most common usage of both functions with one call.
502
503The parameter \c TARGETS specifies a list of targets that intend to load the
504generated \c{.qm} files at run time. If there's only one such target, you may
505directly pass the target's name as the first argument.
506
507The parameter \c SOURCE_TARGETS specifies a list of executable or library
508targets that contain sources with translatable strings. From the sources of
509these targets, \c{.ts} files will be created.
510
511If \c SOURCE_TARGETS is not given, targets are automatically gathered by calling
512\l{qt6_collect_translation_source_targets}{qt_collect_translation_source_targets}
513at the end of the directory scope of \c{PROJECT_SOURCE_DIR}. This functionality
514requires CMake 3.19 or newer. This functionality can be turned off with the
515argument \c{IMMEDIATE_CALL}.
516
517This function will create the target \c{update_translations} that scans all
518source files with \c lupdate and creates and updates the \c{.ts} files.
519
520This function will create the target \c{release_translations} that generates the
521\c{.qm} files from the \c{.ts} files. This target is built by default.
522
523The \c{.ts} files may be specified with the argument \c{TS_FILES}, but it's more
524convenient to let \c qt_add_translations figure out the file paths
525automatically. See \l{Automatic Determination of .ts File Paths} for
526details.
527
528\include cmake-macros.qdoc sources-and-include-dirs
529
530\section1 Automatic Determination of .ts File Paths
531
532The paths of the \c{.ts} files that are used as input for \c qt_add_translations
533can be automatically determined if \l{QT_I18N_TRANSLATED_LANGUAGES} has been
534set. This variable can be conveniently set with \l qt_standard_project_setup.
535
536The following project setup is usually enough:
537
538\snippet cmake-macros/examples.cmake auto_determine_ts_file_paths
539
540This will create the files \c{myproject_de.ts} and \c{myproject_fr.ts} in the
541project's source directory.
542
543By default, the \c{.ts} files are created in \c{CMAKE_CURRENT_SOURCE_DIR}. You
544can change the location by passing a different directory with the \c
545TS_OUTPUT_DIRECTORY argument.
546
547By default, the \c{.ts} file names are constructed from \c{PROJECT_NAME}. You
548can specify a different base name with the \c{TS_FILE_BASE} argument.
549
550\note \c TS_OUTPUT_DIRECTORY and \c TS_FILE_BASE have no effect for explicitly
551specified \c{.ts} files.
552
553Since Qt 6.8, you can specify the \c TS_FILES_OUTPUT_VARIABLE argument to store
554the automatically determined \c{.ts} file paths in a variable.
555
556Before Qt 6.9, the argument \c TS_OUTPUT_DIRECTORY was called \c TS_FILE_DIR.
557That name is still an alias for \c TS_OUTPUT_DIRECTORY to keep older project
558files working.
559
560\include cmake-macros.qdoc plurals-ts-file
561
562A plurals-only \c{.ts} is automatically generated unless the option \c
563NO_GENERATE_PLURALS_TS_FILE is specified.
564
565If you need a full translation of the source language, add it to
566\l{QT_I18N_TRANSLATED_LANGUAGES}
567
568For example,
569\badcode
570project(myapp)
571qt_standard_project_setup(
572 I18N_SOURCE_LANGUAGE en # optional - this is the default
573 I18N_TRANSLATED_LANGUAGES de
574)
575qt_add_executable(myapp ...)
576...
577qt_add_translations(myapp)
578\endcode
579
580creates the full translation file \c{myapp_de.ts} and the plurals-only file
581\c{myapp_en.ts}.
582
583\section1 Options
584
585You can specify the name of the custom target that calls lupdate with the \c
586LUPDATE_TARGET option. Likewise, \c LRELEASE_TARGET controls the name of the
587custom target that drives the call to \c lrelease.
588
589You can set additional options for \l{lupdate} and \l{lrelease} with
590\c{LUPDATE_OPTIONS} and \c{LRELEASE_OPTIONS}. You can find possible options in
591the \l{lupdate options} and \l{lrelease options}.
592
593\section1 Location of generated .qm files
594
595By default, the \c{.qm} files will be placed in the current build directory
596(\c{CMAKE_CURRENT_BINARY_DIR}). Since Qt 6.9 you can use the \c
597QM_OUTPUT_DIRECTORY argument to generate the \c{.qm} files in a different
598directory. Relative paths are considered to be below \c
599CMAKE_CURRENT_BINARY_DIR.
600
601For example, with the following code, the \c{.qm} files are generated
602in a \c{translations} directory below the current build directory.
603
604\snippet cmake-macros/examples.cmake set_qm_output_directory_with_qt_add_translations
605
606For more fine-grained control, you can specify the output directory of
607individual \c{.qm} files by setting the source file property \c{OUTPUT_LOCATION}
608on the corresponding \c{.ts} file. This must happen before calling \c
609qt_add_translations.
610
611The \c OUTPUT_LOCATION source file property overrides the directory passed via
612\c QM_OUTPUT_DIRECTORY.
613
614For example, with the following code, the \c{.qm} files are generated
615in a \c{translations} directory below the current build directory.
616
617\snippet cmake-macros/examples.cmake set_output_location_on_ts_file_with_qt_add_translations
618
619\section1 Embedding Generated .qm Files in Resources
620
621By default, the generated \c{.qm} files are embedded in a Qt resource that will
622be linked into the targets passed with \c{TARGETS}. The files in the resource
623are accessible under the resource prefix \c{"/i18n"}.
624
625You can set the resource prefix with \c{RESOURCE_PREFIX}.
626
627In a static Qt build, when a resource target is created, additional targets can
628be created. You can instruct \c{qt_add_translations} to store these targets in a
629variable, by passing \c{OUTPUT_TARGETS <variable-name>}.
630
631If \c OUTPUT_TARGETS is used, either \c IMMEDIATE_CALL or \c SOURCE_TARGETS must
632be specified.
633
634The automatic resource embedding can be turned off by giving the
635\c{QM_FILES_OUTPUT_VARIABLE} option, followed by the name of a variable in which
636the command should store the list of generated \c{.qm} files.
637
638\section1 \c{qt_add_translations} before Qt 6.7
639
640Before Qt 6.7, this command accepted only one target as the first argument. This
641target was used for both, extracting translatable sources and embedding \c{.qm}
642files.
643
644Since Qt 6.7, the target in the first argument is not used for source extraction
645anymore.
646
647\section1 Merging Qt-provided translations
648
649Since Qt 6.9, you can merge the translations that are provided by Qt into
650application-specific \c{.qm} files. To do that, pass the \c
651MERGE_QT_TRANSLATIONS option to \c{qt_add_translations}. This will determine the
652Qt translation catalogs that belong to the modules used by your project and
653merge them into the \c{.qm} files that are produced by \c{lrelease}.
654
655The Qt translation catalogs are determined by the \c find_package calls in the
656scope of your \c{CMakeLists.txt} where \c qt_add_translations is called. For
657example, the following sequence will merge the catalogs \c qtbase and
658\c{qtmultimedia} into your \c{.qm} files:
659
660\badcode
661find_package(Qt6 COMPONENTS MultimediaWidgets)
662...
663qt_add_translations(my_app MERGE_QT_TRANSLATIONS)
664\endcode
665
666To explicitly specify the catalogs, use the \c QT_TRANSLATION_CATALOGS argument:
667
668\badcode
669qt_add_translations(my_app
670 MERGE_QT_TRANSLATIONS
671 QT_TRANSLATION_CATALOGS qtbase qtmultimedia
672)
673\endcode
674
675If your project supports a language for which Qt doesn't offer its own
676translation, a warning will be issued at configure time. You can suppress this
677warning by setting the CMake variable \c QT_NO_MISSING_CATALOG_LANGUAGE_WARNING
678to \c{ON}.
679
680If the automatic determination of \c{.ts} file names is not used then you must
681ensure that the \l QT_I18N_TRANSLATED_LANGUAGES source file property is set on
682each of the \c{.ts} files.
683
684\section1 Examples
685
686Add a German and a French translation to the target \c{frogger} using
687\c{qt_add_translations}:
688
689\snippet cmake-macros/examples.cmake qt_add_translations_default
690
691This will create the \c{.ts} files \c{frogger_de.ts} and \c{frogger_fr.ts} in
692the source directory. \l lupdate sees the source files of all targets that are
693eligible for translation, according to the rules of \l
694qt_collect_translation_source_targets.
695
696The \c{.qm} files that are created from the \c{.ts} files
697are embedded in the \c frogger_game target under the resource prefix \c{"i18n"}.
698
699The \c qt_add_translations call in the above example is roughly equivalent to
700the following:
701
702\snippet cmake-macros/examples.cmake qt_lupdate_lrelease
703
704\section2 Excluding directories, targets, and sources
705
706You can exclude targets and directories from the automatic collection of source
707targets. The following excludes the target \c helper_lib and everything under
708the \c tests directory. See the \l{directory property
709QT_EXCLUDE_FROM_TRANSLATION} and the \l{target property
710QT_EXCLUDE_FROM_TRANSLATION}.
711
712\badcode
713# <project_root>/CMakeLists.txt
714qt_add_translations(frogger_game)
715\endcode
716
717\badcode
718# <project_root>/src/helper_lib/CMakeLists.txt
719qt_add_library(helper_lib STATIC helpers.cpp)
720set_property(TARGET helper_lib PROPERTY QT_EXCLUDE_FROM_TRANSLATION ON)
721\endcode
722
723\badcode
724# <project_root>/tests/CMakeLists.txt
725add_subdirectory(behavior_tests)
726add_subdirectory(physics_tests)
727set_directory_properties(PROPERTIES QT_EXCLUDE_FROM_TRANSLATION ON)
728\endcode
729
730In the following example, we exclude source files that are part of the \c
731frogger_game target using the \l QT_EXCLUDE_SOURCES_FROM_TRANSLATION target
732property:
733
734\badcode
735qt_add_executable(frogger_game
736 main.cpp
737 3rdparty/jumpsim.cpp
738 3rdparty/frogmath.cpp
739)
740set_property(TARGET frogger_game
741 PROPERTY QT_EXCLUDE_SOURCES_FROM_TRANSLATION "3rdparty/*"
742)
743\endcode
744
745\section2 Explicit specification of source targets
746
747If you don't want to use the automatic collection of source targets you can
748specify the source targets explicitly:
749
750\snippet cmake-macros/examples.cmake qt_add_translations_explicit_source_targets
751
752\section2 Custom resource prefix
753
754Now, let's embed the \c{.qm} files in \c frogger_game and \c
755frogger_level_editor and set a custom resource prefix.
756
757\snippet cmake-macros/examples.cmake qt_add_translations_resource_prefix
758
759\section2 Installing .qm files
760
761Instead of embedding the \c{.qm} files we can install them as regular files:
762
763\snippet cmake-macros/examples.cmake qt_add_translations_install
764
765\section2 Influencing the names of the .ts files
766
767Place the \c{.ts} files in a \c translations directory and change the base name
768to \c frogger_i18n:
769
770\badcode
771qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES de fr)
772...
773qt_add_translations(frogger
774 TS_FILE_BASE froggo
775 TS_OUTPUT_DIRECTORY translations
776)
777\endcode
778
779This creates the following files
780\list
781\li \c translations/froggo_de.ts
782\li \c translations/froggo_fr.ts
783\endlist
784
785You can also specify the paths explicitly:
786\badcode
787qt_add_translations(frogger
788 TS_FILES translations/froggo_de.ts translations/froggo_fr.ts
789)
790\endcode
791
792*/
793
794/*!
795\page qtlinguist-cmake-qt-collect-targets-for-translation.html
796\ingroup cmake-commands-qtlinguisttools
797
798\title qt_collect_translation_source_targets
799\keyword qt6_collect_translation_source_targets
800
801\summary {Collects targets that are eligible for translation.}
802
803\include cmake-find-package-linguisttools.qdocinc
804
805\cmakecommandsince 6.7
806
807\section1 Synopsis
808
809\badcode
810qt_collect_translation_source_targets(out_var [DIRECTORY path])
811\endcode
812
813\versionlessCMakeCommandsNote qt6_collect_translation_source_targets()
814
815\section1 Description
816
817Collect targets that are eligible for translation in the given \c DIRECTORY and
818all subdirectories. If \c DIRECTORY is not specified, start the target
819collection at \c{CMAKE_CURRENT_SOURCE_DIR}.
820
821The command stores the list of targets in the variable that is specified as
822first argument.
823
824\section1 Excluding targets from translation
825
826By default, all non-imported executable and library targets are eligible for
827translation.
828
829Single targets can be excluded by setting the \l{target property
830QT_EXCLUDE_FROM_TRANSLATION}.
831
832Targets below a certain directory can be excluded by setting the \l{directory
833property QT_EXCLUDE_FROM_TRANSLATION}.
834
835\section1 When to call this command
836
837The \c qt_collect_translation_source_targets command reads the
838\l{https://cmake.org/cmake/help/latest/prop_dir/BUILDSYSTEM_TARGETS.html}{BUILDSYSTEM_TARGETS}
839directory properties. As a consequence, it only collects targets that already
840have been created. Targets that are created after \c
841qt_collect_translation_source_targets has been called are not collected.
842
843To collect all targets of the build system, call \c
844qt_collect_translation_source_targets at the end of the top-level \c
845CMakeLists.txt or use \c{cmake_language(DEFER CALL)} to set up i18n at the end
846of the top-level directory scope.
847
848\section1 Examples
849
850Use the result of \c qt_collect_translation_source_targets as input for \c
851qt_add_lupdate.
852
853\snippet cmake-macros/examples.cmake qt_collect_translation_source_targets
854
855With CMake 3.19 and above, you can collect the source targets at the end of the
856directory scope. This way, \c qt_collect_translation_source_targets can be
857called before all targets have been defined. However, you need to exclude the
858tests by setting the directory property \l{directory property
859QT_EXCLUDE_FROM_TRANSLATION}{QT_EXCLUDE_FROM_TRANSLATION} to \c{ON}.
860
861\snippet cmake-macros/examples.cmake qt_collect_translation_source_targets_deferred
862*/