CMake: Add support for overriding the translation domain via a target property

This is useful in environments such as esp-idf, where the component name is something awkward like __esp_idf_main or so.
This commit is contained in:
Simon Hausmann 2025-03-14 11:58:16 +01:00 committed by Simon Hausmann
parent c9bc5b0c16
commit 154d877d9f
2 changed files with 11 additions and 2 deletions

View file

@ -60,6 +60,9 @@ function(SLINT_TARGET_SOURCES target)
set(bundle_translations_prop "$<TARGET_GENEX_EVAL:${target},$<TARGET_PROPERTY:${target},SLINT_BUNDLE_TRANSLATIONS>>")
set(bundle_translations_arg "$<IF:$<STREQUAL:${bundle_translations_prop},>,,--bundle-translations=${bundle_translations_prop}>")
set(translation_domain_prop "$<TARGET_GENEX_EVAL:${target},$<TARGET_PROPERTY:${target},SLINT_TRANSLATION_DOMAIN>>")
set(translation_domain_arg "$<IF:$<STREQUAL:${translation_domain_prop},>,${target},${translation_domain_prop}>")
if (compilation_units GREATER 0)
foreach(cpp_num RANGE 1 ${compilation_units})
list(APPEND cpp_files "${CMAKE_CURRENT_BINARY_DIR}/slint_generated_${_SLINT_BASE_NAME}_${cpp_num}.cpp")
@ -74,7 +77,7 @@ function(SLINT_TARGET_SOURCES target)
--depfile ${CMAKE_CURRENT_BINARY_DIR}/${_SLINT_BASE_NAME}.d
--style ${_SLINT_STYLE}
--embed-resources=${embed}
--translation-domain="${target}"
--translation-domain=${translation_domain_arg}
${_SLINT_CPP_NAMESPACE_ARG}
${_SLINT_CPP_LIBRARY_PATHS_ARG}
${scale_factor_arg}

View file

@ -70,7 +70,7 @@ 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
## 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.
@ -82,3 +82,9 @@ In the following example, the translation files will be bundled from `lang/<lang
```cmake
set_property(TARGET my_application PROPERTY SLINT_BUNDLE_TRANSLATIONS "${CMAKE_CURRENT_SOURCE_DIR}/lang")
```
## Translation Domain
By default, the domain used for translations is the name of the CMake target the `.slint` files are targeted with.
Use the `SLINT_TRANSLATION_DOMAIN` target property to override this and use the specified value as domain, instead.
This is useful in build environments where the target name is given and not suitable, such as esp-idf.