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
8
The following CMake commands are defined when Qt6::LinguistTools is loaded, for instance
9
with
10
11
\code
12
find_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
30
functions \l{qt6_add_lrelease} or \l{qt6_add_translations} instead.
31
32
\section1 Synopsis
33
34
\badcode
35
qt_add_translation(<VAR> file1.ts [file2.ts ...]
36
[OPTIONS ...])
37
\endcode
38
39
\versionlessCMakeCommandsNote qt6_add_translation
40
41
\section1 Description
42
43
Calls \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
48
You can set additional \c{OPTIONS} that should be passed when \c{lrelease} is
49
invoked. You can find possible options in the \l{lrelease}{lrelease documentation}.
50
51
By 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}
53
as a property of the source \c{.ts} file before calling \c qt_add_translation.
54
55
For example, with the following code, the \c{.qm} files are generated
56
in 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
62
Generating \c{helloworld_en.qm}, \c{helloworld_de.qm} in the build
63
directory:
64
65
\snippet cmake-macros/examples.cmake qt_add_translation
66
67
Generating \c{helloworld_en.qm}, \c{helloworld_de.qm} in a \c{l10n}
68
sub-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
85
functions \l{qt6_add_lupdate} or \l{qt6_add_translations} instead.
86
87
\section1 Synopsis
88
89
\badcode
90
qt_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
98
Processes given sources (directories or individual files) to generate
99
Qt Linguist \c{.ts} files. The \c{.ts} files are in turn compiled into \c{.qm}
100
files of the same base name that are stored in the build
101
directory. Paths to the generated \c{.qm} files are added to \c{<VAR>}.
102
103
The translation files to create or update need to have a \c{.ts} suffix. If
104
the given file path is not absolute it is resolved relative to the current
105
source directory. If no \c{.ts} file is passed as an argument, the macro
106
does nothing.
107
108
Any 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.
110
See also the \l{lupdate}{lupdate documentation} on further details.
111
112
\section1 Options
113
114
You can set additional \c{OPTIONS} that should be passed when \c{lupdate} is
115
invoked. You can find possible options in the \l{lupdate}{lupdate documentation}.
116
117
\section1 Examples
118
119
Recursively look up Qt translations from source files in current directory and
120
generate 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}
122
files 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
142
Since 6.7:
143
\badcode
144
qt_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
154
Since 6.2 (deprecated):
155
\badcode
156
qt_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
167
Creates a custom target to generate or update Qt Linguist \c{.ts}
168
files with \l{lupdate}.
169
170
The name of that custom target defaults to \c{${PROJECT_NAME}_lupdate}. Further
171
calls of \c qt_add_lupdate will create target names with an increasing number
172
appended. The custom target name can be specified with the \c LUPDATE_TARGET
173
option.
174
175
With the \c{SOURCE_TARGETS} argument you can specify a list of targets that
176
contain sources with translatable strings. If \c{SOURCE_TARGETS} is not
177
specified, 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}
180
is called to retrieve the list of targets.
181
182
The \c{.ts} files must be specified with the argument \c{TS_FILES}.
183
184
This 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
191
With \c{SOURCES} you can explicitly specify additional source files that contain
192
translatable strings.
193
194
You can use \c{INCLUDE_DIRECTORIES} to explicitly specify include directories
195
for those source files.
196
//! [sources-and-include-dirs]
197
198
\section1 Options
199
200
You can set additional \c{OPTIONS} that should be passed when \c{lupdate} is
201
invoked. You can find possible options in the \l{lupdate}{lupdate
202
documentation}.
203
204
\section1 Umbrella Target
205
206
In 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
210
Pass \c{NO_GLOBAL_TARGET} to \c{qt_add_lupdate} to prevent this behavior.
211
212
The 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
219
strings are written. For handling plural forms correctly, create an additional
220
\c{.ts} file for that language that only contains translatable strings for
221
plural forms. See \l{Handle Plural Forms} for details.
222
223
With \c PLURALS_TS_FILE you can specify the \c{.ts} file for the source
224
language. This file will only contain plural forms.
225
//! [plurals-ts-file]
226
227
\section1 Deprecated Command Signature
228
229
Older versions of \c qt_add_lupdate took a target as the first argument. This is
230
deprecated. Use the \c SOURCE_TARGETS argument instead.
231
232
\section1 Examples
233
234
Add 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
239
You can specify the name of the created target by passing the \c LUPDATE_TARGET
240
argument:
241
242
\badcode
243
qt_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
266
Since 6.7:
267
\badcode
268
qt_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
279
Since 6.2 (deprecated):
280
\badcode
281
qt_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
291
target directory scope requires at least CMake version 3.18.
292
293
\section1 Description
294
295
Creates a custom command to transform \c{.ts} files into \c{.qm} files with
296
\l{lrelease}.
297
298
The execution of the custom command is driven by a custom target that is built
299
by default. The name of that custom target defaults to
300
\c{${PROJECT_NAME}_lrelease}. Further calls of \c qt_add_lrelease will create
301
target names with an increasing number appended. The custom target name can be
302
specified with the \c LRELEASE_TARGET option.
303
304
The \c{.ts} files must be specified with the argument \c{TS_FILES}.
305
306
This 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
312
You can set additional \c{OPTIONS} that should be passed when \c{lrelease} is
313
invoked. You can find possible options in the \l{lrelease}{lrelease
314
documentation}.
315
316
\section1 Location of generated .qm files
317
318
By 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
320
QM_OUTPUT_DIRECTORY argument to generate the \c{.qm} files in a different
321
directory. Relative paths are considered to be below \c
322
CMAKE_CURRENT_BINARY_DIR.
323
324
For example, with the following code, the \c{.qm} files are generated
325
in 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
329
For more fine-grained control, you can specify the output directory of
330
individual \c{.qm} files by setting the source file property \c{OUTPUT_LOCATION}
331
on the corresponding \c{.ts} file. This must happen before calling \c
332
qt_add_lrelease.
333
334
The \c OUTPUT_LOCATION source file property overrides the directory passed via
335
\c QM_OUTPUT_DIRECTORY.
336
337
For example, with the following code, the \c{.qm} files are generated
338
in 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
344
To further process the generated \c{.qm} files, for example to create install
345
rules, \c{qt_add_lrelease} can store the paths of the \c{.qm} files in a
346
variable. Pass \c{QM_FILES_OUTPUT_VARIABLE <variable-name>} to the function for
347
that.
348
349
\section1 Build by Default
350
351
By default, the command creates a custom target that is added to the default
352
build target. This ensures that the \c{.qm} files are always up-to-date when the
353
project is built. This behavior can be turned off with the \c{EXCLUDE_FROM_ALL}
354
argument. In this case, the user must build the \c{${PROJECT_NAME}_lrelease}
355
target manually.
356
357
\section1 Umbrella Target
358
359
In 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
363
Pass \c{NO_GLOBAL_TARGET} to \c{qt_add_lrelease} to prevent this behavior.
364
365
The 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
370
Since Qt 6.9, you can merge the translations that are provided by Qt into
371
application-specific \c{.qm} files. To do that, pass the \c
372
MERGE_QT_TRANSLATIONS option to \c{qt_add_translations}. This will determine the
373
Qt translation catalogs that belong to the modules used by your project and
374
merge them into the \c{.qm} files that are produced by \c{lrelease}.
375
376
The Qt translation catalogs are determined by the \c find_package calls in the
377
scope of your \c{CMakeLists.txt} where \c qt_add_translations is called. For
378
example, the following sequence will merge the catalogs \c qtbase and
379
\c{qtmultimedia} into your \c{.qm} files:
380
381
\badcode
382
find_package(Qt6 COMPONENTS MultimediaWidgets)
383
...
384
qt_add_lrelease(
385
TS_FILES myapp_de.ts myapp_fi.ts
386
MERGE_QT_TRANSLATIONS
387
)
388
\endcode
389
390
To explicitly specify the catalogs, use the \c QT_TRANSLATION_CATALOGS argument:
391
392
\badcode
393
qt_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
400
If your project supports a language for which Qt doesn't offer its own
401
translation, a warning will be issued at configure time. You can suppress this
402
warning by setting the CMake variable \c QT_NO_MISSING_CATALOG_LANGUAGE_WARNING
403
to \c{ON}.
404
405
\section1 Deprecated Command Signature
406
407
Older versions of \c qt_add_lrelease took a target as the first argument. This
408
is still possible but deprecated.
409
410
\section1 Examples
411
412
Add the targets \c{myapp_lrelease} and \c{release_translations} for transforming
413
the given \c{.ts} files into \c{.qm} files. Also, install the generated \c{.qm}
414
files. The target \c{myapp_lrelease} is built by default.
415
416
\badcode
417
project(myapp)
418
...
419
qt_add_lrelease(
420
TS_FILES myapp_de.ts myapp_fr.ts
421
QM_FILES_OUTPUT_VARIABLE qm_files
422
)
423
install(FILES ${qm_files} DESTINATION "translations")
424
\endcode
425
426
You can specify the name of the created target by passing the \c LRELEASE_TARGET
427
argument:
428
429
\badcode
430
qt_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
453
Since Qt 6.7:
454
\badcode
455
qt_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
479
Since Qt 6.2 (deprecated):
480
\badcode
481
qt_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
494
target directory scope requires at least CMake version 3.18.
495
496
\section1 Description
497
498
Creates targets for updating Qt Linguist \c{.ts} files and for transforming them
499
into \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}
501
and aims to offer the most common usage of both functions with one call.
502
503
The parameter \c TARGETS specifies a list of targets that intend to load the
504
generated \c{.qm} files at run time. If there's only one such target, you may
505
directly pass the target's name as the first argument.
506
507
The parameter \c SOURCE_TARGETS specifies a list of executable or library
508
targets that contain sources with translatable strings. From the sources of
509
these targets, \c{.ts} files will be created.
510
511
If \c SOURCE_TARGETS is not given, targets are automatically gathered by calling
512
\l{qt6_collect_translation_source_targets}{qt_collect_translation_source_targets}
513
at the end of the directory scope of \c{PROJECT_SOURCE_DIR}. This functionality
514
requires CMake 3.19 or newer. This functionality can be turned off with the
515
argument \c{IMMEDIATE_CALL}.
516
517
This function will create the target \c{update_translations} that scans all
518
source files with \c lupdate and creates and updates the \c{.ts} files.
519
520
This 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
523
The \c{.ts} files may be specified with the argument \c{TS_FILES}, but it's more
524
convenient to let \c qt_add_translations figure out the file paths
525
automatically. See \l{Automatic Determination of .ts File Paths} for
526
details.
527
528
\include cmake-macros.qdoc sources-and-include-dirs
529
530
\section1 Automatic Determination of .ts File Paths
531
532
The paths of the \c{.ts} files that are used as input for \c qt_add_translations
533
can be automatically determined if \l{QT_I18N_TRANSLATED_LANGUAGES} has been
534
set. This variable can be conveniently set with \l qt_standard_project_setup.
535
536
The following project setup is usually enough:
537
538
\snippet cmake-macros/examples.cmake auto_determine_ts_file_paths
539
540
This will create the files \c{myproject_de.ts} and \c{myproject_fr.ts} in the
541
project's source directory.
542
543
By default, the \c{.ts} files are created in \c{CMAKE_CURRENT_SOURCE_DIR}. You
544
can change the location by passing a different directory with the \c
545
TS_OUTPUT_DIRECTORY argument.
546
547
By default, the \c{.ts} file names are constructed from \c{PROJECT_NAME}. You
548
can 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
551
specified \c{.ts} files.
552
553
Since Qt 6.8, you can specify the \c TS_FILES_OUTPUT_VARIABLE argument to store
554
the automatically determined \c{.ts} file paths in a variable.
555
556
Before Qt 6.9, the argument \c TS_OUTPUT_DIRECTORY was called \c TS_FILE_DIR.
557
That name is still an alias for \c TS_OUTPUT_DIRECTORY to keep older project
558
files working.
559
560
\include cmake-macros.qdoc plurals-ts-file
561
562
A plurals-only \c{.ts} is automatically generated unless the option \c
563
NO_GENERATE_PLURALS_TS_FILE is specified.
564
565
If you need a full translation of the source language, add it to
566
\l{QT_I18N_TRANSLATED_LANGUAGES}
567
568
For example,
569
\badcode
570
project(myapp)
571
qt_standard_project_setup(
572
I18N_SOURCE_LANGUAGE en # optional - this is the default
573
I18N_TRANSLATED_LANGUAGES de
574
)
575
qt_add_executable(myapp ...)
576
...
577
qt_add_translations(myapp)
578
\endcode
579
580
creates the full translation file \c{myapp_de.ts} and the plurals-only file
581
\c{myapp_en.ts}.
582
583
\section1 Options
584
585
You can specify the name of the custom target that calls lupdate with the \c
586
LUPDATE_TARGET option. Likewise, \c LRELEASE_TARGET controls the name of the
587
custom target that drives the call to \c lrelease.
588
589
You 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
591
the \l{lupdate options} and \l{lrelease options}.
592
593
\section1 Location of generated .qm files
594
595
By 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
597
QM_OUTPUT_DIRECTORY argument to generate the \c{.qm} files in a different
598
directory. Relative paths are considered to be below \c
599
CMAKE_CURRENT_BINARY_DIR.
600
601
For example, with the following code, the \c{.qm} files are generated
602
in 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
606
For more fine-grained control, you can specify the output directory of
607
individual \c{.qm} files by setting the source file property \c{OUTPUT_LOCATION}
608
on the corresponding \c{.ts} file. This must happen before calling \c
609
qt_add_translations.
610
611
The \c OUTPUT_LOCATION source file property overrides the directory passed via
612
\c QM_OUTPUT_DIRECTORY.
613
614
For example, with the following code, the \c{.qm} files are generated
615
in 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
621
By default, the generated \c{.qm} files are embedded in a Qt resource that will
622
be linked into the targets passed with \c{TARGETS}. The files in the resource
623
are accessible under the resource prefix \c{"/i18n"}.
624
625
You can set the resource prefix with \c{RESOURCE_PREFIX}.
626
627
In a static Qt build, when a resource target is created, additional targets can
628
be created. You can instruct \c{qt_add_translations} to store these targets in a
629
variable, by passing \c{OUTPUT_TARGETS <variable-name>}.
630
631
If \c OUTPUT_TARGETS is used, either \c IMMEDIATE_CALL or \c SOURCE_TARGETS must
632
be specified.
633
634
The 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
636
the command should store the list of generated \c{.qm} files.
637
638
\section1 \c{qt_add_translations} before Qt 6.7
639
640
Before Qt 6.7, this command accepted only one target as the first argument. This
641
target was used for both, extracting translatable sources and embedding \c{.qm}
642
files.
643
644
Since Qt 6.7, the target in the first argument is not used for source extraction
645
anymore.
646
647
\section1 Merging Qt-provided translations
648
649
Since Qt 6.9, you can merge the translations that are provided by Qt into
650
application-specific \c{.qm} files. To do that, pass the \c
651
MERGE_QT_TRANSLATIONS option to \c{qt_add_translations}. This will determine the
652
Qt translation catalogs that belong to the modules used by your project and
653
merge them into the \c{.qm} files that are produced by \c{lrelease}.
654
655
The Qt translation catalogs are determined by the \c find_package calls in the
656
scope of your \c{CMakeLists.txt} where \c qt_add_translations is called. For
657
example, the following sequence will merge the catalogs \c qtbase and
658
\c{qtmultimedia} into your \c{.qm} files:
659
660
\badcode
661
find_package(Qt6 COMPONENTS MultimediaWidgets)
662
...
663
qt_add_translations(my_app MERGE_QT_TRANSLATIONS)
664
\endcode
665
666
To explicitly specify the catalogs, use the \c QT_TRANSLATION_CATALOGS argument:
667
668
\badcode
669
qt_add_translations(my_app
670
MERGE_QT_TRANSLATIONS
671
QT_TRANSLATION_CATALOGS qtbase qtmultimedia
672
)
673
\endcode
674
675
If your project supports a language for which Qt doesn't offer its own
676
translation, a warning will be issued at configure time. You can suppress this
677
warning by setting the CMake variable \c QT_NO_MISSING_CATALOG_LANGUAGE_WARNING
678
to \c{ON}.
679
680
If the automatic determination of \c{.ts} file names is not used then you must
681
ensure that the \l QT_I18N_TRANSLATED_LANGUAGES source file property is set on
682
each of the \c{.ts} files.
683
684
\section1 Examples
685
686
Add 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
691
This will create the \c{.ts} files \c{frogger_de.ts} and \c{frogger_fr.ts} in
692
the source directory. \l lupdate sees the source files of all targets that are
693
eligible for translation, according to the rules of \l
694
qt_collect_translation_source_targets.
695
696
The \c{.qm} files that are created from the \c{.ts} files
697
are embedded in the \c frogger_game target under the resource prefix \c{"i18n"}.
698
699
The \c qt_add_translations call in the above example is roughly equivalent to
700
the following:
701
702
\snippet cmake-macros/examples.cmake qt_lupdate_lrelease
703
704
\section2 Excluding directories, targets, and sources
705
706
You can exclude targets and directories from the automatic collection of source
707
targets. The following excludes the target \c helper_lib and everything under
708
the \c tests directory. See the \l{directory property
709
QT_EXCLUDE_FROM_TRANSLATION} and the \l{target property
710
QT_EXCLUDE_FROM_TRANSLATION}.
711
712
\badcode
713
# <project_root>/CMakeLists.txt
714
qt_add_translations(frogger_game)
715
\endcode
716
717
\badcode
718
# <project_root>/src/helper_lib/CMakeLists.txt
719
qt_add_library(helper_lib STATIC helpers.cpp)
720
set_property(TARGET helper_lib PROPERTY QT_EXCLUDE_FROM_TRANSLATION ON)
721
\endcode
722
723
\badcode
724
# <project_root>/tests/CMakeLists.txt
725
add_subdirectory(behavior_tests)
726
add_subdirectory(physics_tests)
727
set_directory_properties(PROPERTIES QT_EXCLUDE_FROM_TRANSLATION ON)
728
\endcode
729
730
In the following example, we exclude source files that are part of the \c
731
frogger_game target using the \l QT_EXCLUDE_SOURCES_FROM_TRANSLATION target
732
property:
733
734
\badcode
735
qt_add_executable(frogger_game
736
main.cpp
737
3rdparty/jumpsim.cpp
738
3rdparty/frogmath.cpp
739
)
740
set_property(TARGET frogger_game
741
PROPERTY QT_EXCLUDE_SOURCES_FROM_TRANSLATION "3rdparty/*"
742
)
743
\endcode
744
745
\section2 Explicit specification of source targets
746
747
If you don't want to use the automatic collection of source targets you can
748
specify the source targets explicitly:
749
750
\snippet cmake-macros/examples.cmake qt_add_translations_explicit_source_targets
751
752
\section2 Custom resource prefix
753
754
Now, let's embed the \c{.qm} files in \c frogger_game and \c
755
frogger_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
761
Instead 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
767
Place the \c{.ts} files in a \c translations directory and change the base name
768
to \c frogger_i18n:
769
770
\badcode
771
qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES de fr)
772
...
773
qt_add_translations(frogger
774
TS_FILE_BASE froggo
775
TS_OUTPUT_DIRECTORY translations
776
)
777
\endcode
778
779
This creates the following files
780
\list
781
\li \c translations/froggo_de.ts
782
\li \c translations/froggo_fr.ts
783
\endlist
784
785
You can also specify the paths explicitly:
786
\badcode
787
qt_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
810
qt_collect_translation_source_targets(out_var [DIRECTORY path])
811
\endcode
812
813
\versionlessCMakeCommandsNote qt6_collect_translation_source_targets()
814
815
\section1 Description
816
817
Collect targets that are eligible for translation in the given \c DIRECTORY and
818
all subdirectories. If \c DIRECTORY is not specified, start the target
819
collection at \c{CMAKE_CURRENT_SOURCE_DIR}.
820
821
The command stores the list of targets in the variable that is specified as
822
first argument.
823
824
\section1 Excluding targets from translation
825
826
By default, all non-imported executable and library targets are eligible for
827
translation.
828
829
Single targets can be excluded by setting the \l{target property
830
QT_EXCLUDE_FROM_TRANSLATION}.
831
832
Targets below a certain directory can be excluded by setting the \l{directory
833
property QT_EXCLUDE_FROM_TRANSLATION}.
834
835
\section1 When to call this command
836
837
The \c qt_collect_translation_source_targets command reads the
838
\l{https://cmake.org/cmake/help/latest/prop_dir/BUILDSYSTEM_TARGETS.html}{BUILDSYSTEM_TARGETS}
839
directory properties. As a consequence, it only collects targets that already
840
have been created. Targets that are created after \c
841
qt_collect_translation_source_targets has been called are not collected.
842
843
To collect all targets of the build system, call \c
844
qt_collect_translation_source_targets at the end of the top-level \c
845
CMakeLists.txt or use \c{cmake_language(DEFER CALL)} to set up i18n at the end
846
of the top-level directory scope.
847
848
\section1 Examples
849
850
Use the result of \c qt_collect_translation_source_targets as input for \c
851
qt_add_lupdate.
852
853
\snippet cmake-macros/examples.cmake qt_collect_translation_source_targets
854
855
With CMake 3.19 and above, you can collect the source targets at the end of the
856
directory scope. This way, \c qt_collect_translation_source_targets can be
857
called before all targets have been defined. However, you need to exclude the
858
tests by setting the directory property \l{directory property
859
QT_EXCLUDE_FROM_TRANSLATION}{QT_EXCLUDE_FROM_TRANSLATION} to \c{ON}.
860
861
\snippet cmake-macros/examples.cmake qt_collect_translation_source_targets_deferred
862
*/
qttools
src
linguist
linguist
doc
cmake-macros.qdoc
Generated on
for Qt by
1.14.0