Pass the translation domain to the runtime

For rust, it uses the crate name, for others, it needs to be passed in
the comment line
This commit is contained in:
Olivier Goffart 2023-04-27 13:07:43 +02:00 committed by Olivier Goffart
parent b997d1e8b5
commit ce25fb65a6
9 changed files with 34 additions and 3 deletions

View file

@ -32,6 +32,7 @@ function(SLINT_TARGET_SOURCES target)
-o ${_SLINT_BASE_NAME_REL}.h --depfile ${_SLINT_BASE_NAME_REL}.d
--style ${_SLINT_STYLE}
--embed-resources=${embed}
--translation-domain="${target}"
DEPENDS Slint::slint-compiler ${_SLINT_ABSOLUTE}
COMMENT "Generating ${_SLINT_BASE_NAME}.h"
DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${_SLINT_BASE_NAME}.d
@ -46,6 +47,7 @@ function(SLINT_TARGET_SOURCES target)
-o ${CMAKE_CURRENT_BINARY_DIR}/${_SLINT_BASE_NAME}.h
--style ${_SLINT_STYLE}
--embed-resources=${embed}
--translation-domain="${target}"
DEPENDS Slint::slint-compiler ${_SLINT_ABSOLUTE} ${ALL_SLINTS}
COMMENT "Generating ${_SLINT_BASE_NAME}.h"
)

View file

@ -298,6 +298,8 @@ pub fn compile_with_config(
}
let mut compiler_config = config.config;
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").ok();
let mut rerun_if_changed = String::new();
if std::env::var_os("SLINT_STYLE").is_none() && compiler_config.style.is_none() {

View file

@ -336,6 +336,7 @@ pub fn slint(stream: TokenStream) -> TokenStream {
//println!("{:#?}", syntax_node);
let mut compiler_config =
CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Rust);
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").ok();
if std::env::var_os("SLINT_STYLE").is_none() {
// This file is written by the i-slint-backend-selector's built script.

View file

@ -77,6 +77,9 @@ pub struct CompilerConfiguration {
/// expose the accessible role and properties
pub accessibility: bool,
/// The domain used as one of the parameter to the translate function
pub translation_domain: Option<String>,
}
impl CompilerConfiguration {
@ -128,6 +131,7 @@ impl CompilerConfiguration {
inline_all_elements,
scale_factor,
accessibility: true,
translation_domain: None,
}
}
}

View file

@ -525,6 +525,11 @@ impl Expression {
return Expression::Invalid;
};
let domain = ctx
.type_loader
.and_then(|tl| tl.compiler_config.translation_domain.clone())
.unwrap_or_default();
let subs = node.Expression().map(|n| {
Expression::from_expression_node(n.clone(), ctx).maybe_convert_to(
Type::String,
@ -541,7 +546,7 @@ impl Expression {
arguments: vec![
Expression::StringLiteral(string),
Expression::StringLiteral(String::new()), // TODO
Expression::StringLiteral(String::new()), // TODO
Expression::StringLiteral(domain),
Expression::Array { element_ty: Type::String, values: subs.collect() },
],
source_location: Some(node.to_source_location()),

View file

@ -480,6 +480,11 @@ impl ComponentCompiler {
self.config.style.as_ref()
}
/// The domain used for translations
pub fn set_translation_domain(&mut self, domain: String) {
self.config.translation_domain = Some(domain);
}
/// Sets the callback that will be invoked when loading imported .slint files. The specified
/// `file_loader_callback` parameter will be called with a canonical file path as argument
/// and is expected to return a future that, when resolved, provides the source code of the

View file

@ -724,12 +724,12 @@ pub struct Diagnostic {
#[repr(C)]
#[cfg(target_pointer_width = "64")]
pub struct ComponentCompilerOpaque([usize; 13]);
pub struct ComponentCompilerOpaque([usize; 16]);
#[repr(C)]
#[cfg(target_pointer_width = "32")]
#[repr(align(8))]
pub struct ComponentCompilerOpaque([usize; 16]);
pub struct ComponentCompilerOpaque([usize; 19]);
/// Asserts that ComponentCompilerOpaque is as large as ComponentCompiler and has the same alignment, to make transmute safe.
const _: [(); std::mem::size_of::<ComponentCompilerOpaque>()] =

View file

@ -50,6 +50,10 @@ struct Cli {
/// Sets the output file ('-' for stdout)
#[arg(name = "file to generate", short = 'o', default_value = "-", action)]
output: std::path::PathBuf,
/// Translation domain
#[arg(long = "translation-domain", action)]
translation_domain: Option<String>,
}
fn main() -> std::io::Result<()> {
@ -63,6 +67,7 @@ fn main() -> std::io::Result<()> {
std::process::exit(-1);
}
let mut compiler_config = CompilerConfiguration::new(args.format);
compiler_config.translation_domain = args.translation_domain;
// Override defaults from command line:
if let Some(embed) = args.embed_resources {

View file

@ -52,6 +52,10 @@ struct Cli {
/// and so on.
#[arg(long, value_names(&["callback", "handler"]), number_of_values = 2, action)]
on: Vec<String>,
/// Translation domain
#[arg(long = "translation-domain", action)]
translation_domain: Option<String>,
}
thread_local! {static CURRENT_INSTANCE: std::cell::RefCell<Option<ComponentInstance>> = Default::default();}
@ -139,6 +143,9 @@ fn init_compiler(
fswatcher: Option<Arc<Mutex<notify::RecommendedWatcher>>>,
) -> slint_interpreter::ComponentCompiler {
let mut compiler = slint_interpreter::ComponentCompiler::default();
if let Some(domain) = args.translation_domain.clone() {
compiler.set_translation_domain(domain);
}
compiler.set_include_paths(args.include_paths.clone());
if let Some(style) = &args.style {
compiler.set_style(style.clone());