C++: allow to configure bundled translation

This commit is contained in:
Olivier Goffart 2024-11-18 15:57:08 +01:00 committed by GitHub
parent fb6ab7a1b8
commit 014b58c81a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 41 additions and 9 deletions

View file

@ -1,16 +1,15 @@
# CMake Reference
<!-- Copyright © SixtyFPS GmbH <info@slint.dev> ; SPDX-License-Identifier: MIT -->
## `slint_target_sources`
```
slint_target_sources(<target> <files>.... [NAMESPACE namespace] [LIBRARY_PATHS name1=lib1 name2=lib2 ...] [COMPILATION_UNITS num])
```
Use this function to tell cmake about the .slint files of your application, similar to the builtin cmake [target_sources](https://cmake.org/cmake/help/latest/command/target_sources.html) function.
Use this function to tell CMake about the .slint files of your application, similar to the builtin cmake [target_sources](https://cmake.org/cmake/help/latest/command/target_sources.html) function.
The function takes care of running the slint-compiler to convert `.slint` files to `.h` files in the build directory,
and extend the include directories of your target so that the generated file is found when including it in your application.
and extend the include directories of your target so that the generated file is found when including it in your application.
The optional `NAMESPACE` argument will put the generated components in the given C++ namespace.
@ -25,14 +24,14 @@ be redirected to the specified path.
```cmake
add_executable(my_application main.cpp)
target_link_libraries(my_application PRIVATE Slint::Slint)
slint_target_sources(my_application the_window.slint
slint_target_sources(my_application the_window.slint
NAMESPACE ui
LIBRARY_PATHS mycomponentlib=/path/to/customcomponents
)
```
By default, a `.slint` file is compiled to a `.h` file for inclusion in your application's business logic code, and a `.cpp` file with code generated by
the slint-compier. If you want to speed up compilation of the generated `.cpp` file, then you can pass the `COMPILATION_UNITS` argument with a value greater
the slint-compiler. If you want to speed up compilation of the generated `.cpp` file, then you can pass the `COMPILATION_UNITS` argument with a value greater
than 1 to create multiple `.cpp` files. These can be compiled in parallel, which might speed up overall build times. However, splitting the generated code
across multiple `.cpp` files decreases the compiler's visibility and thus ability to perform optimizations. You can also pass `COMPILATION_UNITS 0` to generate
only one single `.h` file.
@ -67,3 +66,16 @@ set_property(TARGET my_application PROPERTY SLINT_SCALE_FACTOR 2.0)
A scale factor specified this way will also be used to pre-scale images and glyphs when used in combination
with [Resource Embedding](#resource-embedding).
## Bundle translations
Translations can either be done using `gettext` at runtime, or by bundling all the translated strings
directly into the binary, by embedding them in the generated C++ code.
If you want to bundle translations, you need to set the `SLINT_BUNDLE_TRANSLATIONS` target property
to point to a directory containing translations. The translations must be in the gettext `.po` format.
In the following example, the translation files will be bundled from `lang/<lang>/LC_MESSAGES/my_application.po`
```cmake
set_property(TARGET my_application PROPERTY SLINT_BUNDLE_TRANSLATIONS "${CMAKE_CURRENT_SOURCE_DIR}/lang")
```