wgui: basic i18n support, refactoring: use LayoutState, translation framework (LLM-based generator)

This commit is contained in:
Aleksander
2025-08-02 23:31:23 +02:00
parent 4e46c45bcf
commit eaa81450b5
45 changed files with 916 additions and 223 deletions

View File

@@ -1,6 +1,7 @@
use crate::{
components::button,
drawing::Color,
i18n::Translation,
layout::WidgetID,
parser::{
ParserContext, ParserFile, iter_attribs,
@@ -18,8 +19,7 @@ pub fn parse_component_button<'a, U1, U2>(
let mut color = Color::new(1.0, 1.0, 1.0, 1.0);
let mut border_color: Option<Color> = None;
let mut round = WLength::Units(4.0);
let mut text = String::default();
let mut translation = Translation::default();
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
let text_style = parse_text_style(&attribs);
@@ -28,7 +28,10 @@ pub fn parse_component_button<'a, U1, U2>(
for (key, value) in attribs {
match key.as_ref() {
"text" => {
text = String::from(value.as_ref());
translation = Translation::from_raw_text(&value);
}
"translation" => {
translation = Translation::from_translation_key(&value);
}
"round" => {
parse_round(&value, &mut round);
@@ -59,7 +62,7 @@ pub fn parse_component_button<'a, U1, U2>(
button::Params {
color,
border_color: border_color.unwrap(),
text: &text,
text: translation,
style,
text_style,
round,

View File

@@ -11,6 +11,7 @@ use crate::{
components::Component,
drawing::{self},
event::EventListenerCollection,
globals::WguiGlobals,
layout::{Layout, WidgetID},
parser::{
component_button::parse_component_button, component_slider::parse_component_slider,
@@ -631,11 +632,11 @@ pub fn parse_from_assets<U1, U2>(
}
pub fn new_layout_from_assets<U1, U2>(
assets: Box<dyn AssetProvider>,
globals: WguiGlobals,
listeners: &mut EventListenerCollection<U1, U2>,
path: &str,
) -> anyhow::Result<(Layout, ParserState)> {
let mut layout = Layout::new(assets)?;
let mut layout = Layout::new(globals)?;
let widget = layout.root_widget;
let state = parse_from_assets(&mut layout, listeners, widget, path)?;
Ok((layout, state))
@@ -650,7 +651,7 @@ fn get_doc_from_path<U1, U2>(
ctx: &mut ParserContext<U1, U2>,
path: &Path,
) -> anyhow::Result<(ParserFile, roxmltree::NodeId)> {
let xml = assets_path_to_xml(&mut ctx.layout.assets, path)?;
let xml = assets_path_to_xml(&mut ctx.layout.state.globals.assets(), path)?;
let document = Rc::new(XmlDocument::new(xml, |xml| {
let opt = roxmltree::ParsingOptions {
allow_dtd: true,

View File

@@ -1,4 +1,5 @@
use crate::{
i18n::Translation,
layout::WidgetID,
parser::{
ParserContext, ParserFile, iter_attribs, parse_children, parse_universal,
@@ -22,15 +23,20 @@ pub fn parse_widget_label<'a, U1, U2>(
for (key, value) in attribs {
match &*key {
"text" => {
params.content = String::from(value.as_ref());
params.content = Translation::from_raw_text(&value);
}
"translation" => params.content = Translation::from_translation_key(&value),
_ => {}
}
}
let (new_id, _) = ctx
.layout
.add_child(parent_id, TextLabel::create(params)?, style)?;
let globals = ctx.layout.state.globals.clone();
let mut i18n = globals.i18n();
let (new_id, _) =
ctx
.layout
.add_child(parent_id, TextLabel::create(&mut i18n, params)?, style)?;
parse_universal(file, ctx, node, new_id)?;
parse_children(file, ctx, node, new_id)?;

View File

@@ -23,13 +23,14 @@ pub fn parse_widget_sprite<'a, U1, U2>(
for (key, value) in attribs {
match key.as_ref() {
"src" => {
glyph = match CustomGlyphContent::from_assets(&mut ctx.layout.assets, &value) {
Ok(glyph) => Some(glyph),
Err(e) => {
log::warn!("failed to load {}: {}", value, e);
None
glyph =
match CustomGlyphContent::from_assets(&mut ctx.layout.state.globals.assets(), &value) {
Ok(glyph) => Some(glyph),
Err(e) => {
log::warn!("failed to load {value}: {e}");
None
}
}
}
}
"src_ext" => {
if std::fs::exists(value.as_ref()).unwrap_or(false) {