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

@ -57,6 +57,9 @@ function(SLINT_TARGET_SOURCES target)
set(scale_factor_target_prop "$<TARGET_GENEX_EVAL:${target},$<TARGET_PROPERTY:${target},SLINT_SCALE_FACTOR>>") set(scale_factor_target_prop "$<TARGET_GENEX_EVAL:${target},$<TARGET_PROPERTY:${target},SLINT_SCALE_FACTOR>>")
set(scale_factor_arg "$<IF:$<STREQUAL:${scale_factor_target_prop},>,,--scale-factor=${scale_factor_target_prop}>") set(scale_factor_arg "$<IF:$<STREQUAL:${scale_factor_target_prop},>,,--scale-factor=${scale_factor_target_prop}>")
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}>")
if (compilation_units GREATER 0) if (compilation_units GREATER 0)
foreach(cpp_num RANGE 1 ${compilation_units}) foreach(cpp_num RANGE 1 ${compilation_units})
list(APPEND cpp_files "${CMAKE_CURRENT_BINARY_DIR}/slint_generated_${_SLINT_BASE_NAME}_${cpp_num}.cpp") list(APPEND cpp_files "${CMAKE_CURRENT_BINARY_DIR}/slint_generated_${_SLINT_BASE_NAME}_${cpp_num}.cpp")
@ -75,6 +78,7 @@ function(SLINT_TARGET_SOURCES target)
${_SLINT_CPP_NAMESPACE_ARG} ${_SLINT_CPP_NAMESPACE_ARG}
${_SLINT_CPP_LIBRARY_PATHS_ARG} ${_SLINT_CPP_LIBRARY_PATHS_ARG}
${scale_factor_arg} ${scale_factor_arg}
${bundle_translations_arg}
${cpp_files_arg} ${cpp_files_arg}
DEPENDS Slint::slint-compiler ${_SLINT_ABSOLUTE} DEPENDS Slint::slint-compiler ${_SLINT_ABSOLUTE}
COMMENT "Generating ${_SLINT_BASE_NAME}.h" COMMENT "Generating ${_SLINT_BASE_NAME}.h"

View file

@ -1,14 +1,13 @@
# CMake Reference # CMake Reference
<!-- Copyright © SixtyFPS GmbH <info@slint.dev> ; SPDX-License-Identifier: MIT --> <!-- Copyright © SixtyFPS GmbH <info@slint.dev> ; SPDX-License-Identifier: MIT -->
## `slint_target_sources` ## `slint_target_sources`
``` ```
slint_target_sources(<target> <files>.... [NAMESPACE namespace] [LIBRARY_PATHS name1=lib1 name2=lib2 ...] [COMPILATION_UNITS num]) 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, 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.
@ -32,7 +31,7 @@ slint_target_sources(my_application the_window.slint
``` ```
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 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 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 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. 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 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). 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")
```

View file

@ -13,7 +13,9 @@ target_link_libraries(printerdemo PRIVATE Slint::Slint)
slint_target_sources(printerdemo ../ui/printerdemo.slint) slint_target_sources(printerdemo ../ui/printerdemo.slint)
find_package(Intl) find_package(Intl)
if(Intl_FOUND) if(Intl_FOUND AND SLINT_FEATURE_GETTEXT)
target_compile_definitions(printerdemo PRIVATE HAVE_GETTEXT SRC_DIR="${CMAKE_CURRENT_SOURCE_DIR}") target_compile_definitions(printerdemo PRIVATE HAVE_GETTEXT SRC_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(printerdemo PRIVATE Intl::Intl) target_link_libraries(printerdemo PRIVATE Intl::Intl)
else()
set_property(TARGET printerdemo PROPERTY SLINT_BUNDLE_TRANSLATIONS "${CMAKE_CURRENT_SOURCE_DIR}/../lang")
endif() endif()

View file

@ -82,6 +82,11 @@ int main()
setenv("LANGUAGE", langs[l], true); setenv("LANGUAGE", langs[l], true);
slint::update_all_translations(); slint::update_all_translations();
}); });
#else
printer_demo->global<PrinterSettings>().on_change_language([](int l) {
static const char *langs[] = { "", "fr" };
slint::select_bundled_translation(langs[l]);
});
#endif #endif
printer_demo->run(); printer_demo->run();

View file

@ -23,7 +23,7 @@ software-renderer = ["i-slint-compiler/software-renderer"]
default = ["software-renderer"] default = ["software-renderer"]
[dependencies] [dependencies]
i-slint-compiler = { workspace = true, features = ["default", "display-diagnostics", "cpp", "rust"]} i-slint-compiler = { workspace = true, features = ["default", "display-diagnostics", "bundle-translations", "cpp", "rust"]}
clap = { workspace = true } clap = { workspace = true }
proc-macro2 = "1.0.11" proc-macro2 = "1.0.11"

View file

@ -67,6 +67,12 @@ struct Cli {
#[arg(long = "translation-domain", action)] #[arg(long = "translation-domain", action)]
translation_domain: Option<String>, translation_domain: Option<String>,
/// Bundle translations from the specified path.
/// Translations files should be in the gettext .po format and should be found in
/// `<path>/<lang>/LC_MESSAGES/<domain>.po`
#[arg(long = "bundle-translations", name = "path", action)]
bundle_translations: Option<std::path::PathBuf>,
/// C++ namespace /// C++ namespace
#[arg(long = "cpp-namespace", name = "C++ namespace")] #[arg(long = "cpp-namespace", name = "C++ namespace")]
cpp_namespace: Option<String>, cpp_namespace: Option<String>,
@ -142,6 +148,9 @@ fn main() -> std::io::Result<()> {
if let Some(constant_scale_factor) = args.scale_factor { if let Some(constant_scale_factor) = args.scale_factor {
compiler_config.const_scale_factor = constant_scale_factor; compiler_config.const_scale_factor = constant_scale_factor;
} }
if let Some(path) = args.bundle_translations {
compiler_config.translation_path_bundle = Some(path);
}
let syntax_node = syntax_node.expect("diags contained no compilation errors"); let syntax_node = syntax_node.expect("diags contained no compilation errors");
let (doc, diag, loader) = let (doc, diag, loader) =
spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config)); spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config));