WIP: formatting implementation of translate in rust and C++

This commit is contained in:
Olivier Goffart 2023-04-18 15:47:14 +02:00 committed by Olivier Goffart
parent a3855f1bcb
commit cf746ddf8d
8 changed files with 80 additions and 4 deletions

View file

@ -1471,6 +1471,15 @@ public:
}
};
inline SharedString translate(const SharedString &original, const SharedString &context,
const SharedString &domain,
cbindgen_private::Slice<SharedString> arguments)
{
SharedString result = original;
cbindgen_private::slint_translate(&result, &context, &domain, arguments);
return result;
}
} // namespace private_api
#if !defined(DOXYGEN)

View file

@ -144,6 +144,16 @@ pub fn create_window_adapter(
i_slint_backend_selector::with_platform(|b| b.create_window_adapter())
}
/// Wrapper around i_slint_core::translations::translate for the generated code
pub fn translate(
origin: SharedString,
context: SharedString,
domain: SharedString,
args: Slice<SharedString>,
) -> SharedString {
i_slint_core::translations::translate(&origin, &context, &domain, args.as_slice())
}
/// internal re_exports used by the macro generated
pub mod re_exports {
pub use alloc::boxed::Box;

View file

@ -16,7 +16,7 @@ use std::rc::{Rc, Weak};
pub use crate::namedreference::NamedReference;
pub use crate::passes::resolving;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
/// A function built into the run-time
pub enum BuiltinFunction {
GetWindowScaleFactor,

View file

@ -2834,7 +2834,7 @@ fn compile_builtin_function_call(
}
}
BuiltinFunction::Translate => {
format!("slint::private_api::translate({});", a.join(","))
format!("slint::private_api::translate({})", a.join(","))
}
}
}

View file

@ -2442,7 +2442,7 @@ fn compile_builtin_function_call(
quote!(slint::private_unstable_api::re_exports::WindowInner::from_pub(#window_adapter_tokens.window()).set_text_input_focused(#(#a)*))
}
BuiltinFunction::Translate => {
todo!("BuiltinFunction::Translate in rust")
quote!(slint::private_unstable_api::translate(#(#a),*))
}
}
}

View file

@ -117,7 +117,13 @@ pub fn lower_expression(
lower_show_popup(arguments, ctx)
}
tree_Expression::BuiltinFunctionReference(f, _) => {
let arguments = arguments.iter().map(|e| lower_expression(e, ctx)).collect::<_>();
let mut arguments =
arguments.iter().map(|e| lower_expression(e, ctx)).collect::<Vec<_>>();
if *f == BuiltinFunction::Translate {
if let llr_Expression::Array { as_model, .. } = &mut arguments[3] {
*as_model = false;
}
}
llr_Expression::BuiltinFunctionCall { function: f.clone(), arguments }
}
tree_Expression::CallbackReference(nr, _) => {

View file

@ -147,3 +147,23 @@ pub fn translate(
write!(output, "{}", formatter::format(original, arguments)).unwrap();
output
}
#[cfg(feature = "ffi")]
mod ffi {
#![allow(unsafe_code)]
use super::*;
use crate::slice::Slice;
#[no_mangle]
/// Returns a nul-terminated pointer for this string.
/// The returned value is owned by the string, and should not be used after any
/// mutable function have been called on the string, and must not be freed.
pub extern "C" fn slint_translate(
to_translate: &mut SharedString,
context: &SharedString,
domain: &SharedString,
arguments: Slice<SharedString>,
) {
*to_translate = translate(to_translate.as_str(), &context, &domain, arguments.as_slice())
}
}

31
tests/cases/expr/tr.slint Normal file
View file

@ -0,0 +1,31 @@
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
TestCase := Rectangle {
property <int> int_value: 42;
property <string> t1: @tr("Hello World{{}}.");
property <string> t2: @tr("Hello {}.", "World");
property <string> t3: @tr("{} Hello {}", int_value, "World");
property <string> t4: @tr("{1} Hello {0}🌍", @tr("World"), int_value + 1);
property <bool> test: t1 == "Hello World{}." && t2 == "Hello World." && t3 == "42 Hello World" && t4 == "43 Hello World🌍";
}
/*
```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert(instance.get_test());
```
```rust
let instance = TestCase::new().unwrap();
assert!(instance.get_test());
```
```js
var instance = new slint.TestCase({});
assert(instance.test);
```
*/