From 034bda502a86f9457d1122bd8e85acdd77405972 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 11 Mar 2024 09:09:53 -0400 Subject: [PATCH] Remove FluentErgo from the l10n stack --- kifu/gtk/src/messages.rs | 43 ++++++++++++++++++++++++------------ l10n/src/bin/codegen-rust.rs | 19 +++++++++++----- l10n/src/lib.rs | 24 ++++++++++++-------- 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/kifu/gtk/src/messages.rs b/kifu/gtk/src/messages.rs index 9b7cc56..6d72d9b 100644 --- a/kifu/gtk/src/messages.rs +++ b/kifu/gtk/src/messages.rs @@ -1,7 +1,6 @@ // This file was autogenerated from l10n-codegen-rust. Edits will be lost // on next generation. use l10n::Message; -use fluent_ergonomics::FluentErgo; use fluent::FluentArgs; pub struct Hello { @@ -9,24 +8,25 @@ pub struct Hello { } impl Message for Hello { - fn localize(&self, bundle: &FluentErgo) -> String { + fn msgid(&self) -> &str { + "hello" + } + + fn args(&self) -> Option { let mut args = FluentArgs::new(); args.set("name", self.name.clone()); - bundle.tr("hello", Some(&args)).unwrap() - } -} - -pub struct NothingHere; -impl Message for NothingHere { - fn localize(&self, bundle: &FluentErgo) -> String { - bundle.tr("nothing-here", None).unwrap() + Some(args) } } pub struct Welcome; impl Message for Welcome { - fn localize(&self, bundle: &FluentErgo) -> String { - bundle.tr("welcome", None).unwrap() + fn msgid(&self) -> &str { + "welcome" + } + + fn args(&self) -> Option { + None } } @@ -35,9 +35,24 @@ pub struct GamesInDatabase { } impl Message for GamesInDatabase { - fn localize(&self, bundle: &FluentErgo) -> String { + fn msgid(&self) -> &str { + "games-in-database" + } + + fn args(&self) -> Option { let mut args = FluentArgs::new(); args.set("count", self.count.clone()); - bundle.tr("games-in-database", Some(&args)).unwrap() + Some(args) + } +} + +pub struct NothingHere; +impl Message for NothingHere { + fn msgid(&self) -> &str { + "nothing-here" + } + + fn args(&self) -> Option { + None } } diff --git a/l10n/src/bin/codegen-rust.rs b/l10n/src/bin/codegen-rust.rs index 69aefcb..b60a869 100644 --- a/l10n/src/bin/codegen-rust.rs +++ b/l10n/src/bin/codegen-rust.rs @@ -81,8 +81,12 @@ impl Message { struct_strs.push(format!("pub struct {}; ", self.name.to_case(Case::Pascal))); struct_strs.push(format!("impl Message for {} {{ - fn localize(&self, bundle: &FluentErgo) -> String {{ - bundle.tr(\"{}\", None).unwrap() + fn msgid(&self) -> &str {{ + \"{}\" + }} + + fn args(&self) -> Option {{ + None }} }}", self.name.to_case(Case::Pascal), @@ -114,15 +118,19 @@ impl Message { struct_strs.push(format!( "impl Message for {} {{ - fn localize(&self, bundle: &FluentErgo) -> String {{ + fn msgid(&self) -> &str {{ + \"{}\" + }} + + fn args(&self) -> Option {{ let mut args = FluentArgs::new(); {} - bundle.tr(\"{}\", Some(&args)).unwrap() + Some(args) }} }}", self.name.to_case(Case::Pascal), - parameters, self.name, + parameters, )); struct_strs.join("\n") @@ -144,7 +152,6 @@ fn main() { println!("// This file was autogenerated from l10n-codegen-rust. Edits will be lost // on next generation."); println!("use l10n::Message;"); - println!("use fluent_ergonomics::FluentErgo;"); println!("use fluent::FluentArgs;"); let messages = messages diff --git a/l10n/src/lib.rs b/l10n/src/lib.rs index 54c7844..b970bc8 100644 --- a/l10n/src/lib.rs +++ b/l10n/src/lib.rs @@ -68,12 +68,13 @@ impl From for L10NError { // However, I have not found a mechanism in Fluent to identify all of the placeholders within a // message, so I'm not even sure that I can automate this code generation. pub trait Message { - fn localize(&self, bundle: &FluentErgo) -> String; + fn msgid(&self) -> &str; + fn args(&self) -> Option; } pub struct L10N { messages_root: std::path::PathBuf, - message_bundle: FluentErgo, + message_bundles: Vec>, locales: NonEmptyList, zone: chrono_tz::Tz, @@ -94,7 +95,8 @@ impl L10N { let english_phrases = FluentResource::try_new */ - let message_bundle = { + let message_bundles = { + /* let mut english_messages = messages_root.clone(); english_messages.push("en-US.ftl"); @@ -102,12 +104,14 @@ impl L10N { let mut messages = FluentErgo::new(&[langid.clone()]); let _ = messages.add_from_file(langid, &english_messages); - messages + vec![messages] + */ + vec![] }; Self { messages_root, - message_bundle, + message_bundles, locales, zone, } @@ -156,12 +160,14 @@ impl L10N { // parameters. In an ideal world, neither of these can be incorrect. Messages are all checked // at compile time, as are their parameters. That implies an enumeration, with one element per // message, and with each element knowing its parameters. - pub fn messages(&self) -> FluentErgo { - self.message_bundle.clone() - } + // pub fn messages(&self) -> Vec> { + // self.message_bundles.clone() + // } pub fn tr(&self, message: impl Message) -> String { - message.localize(&self.message_bundle) + let msg = self.message_bundles[0].get_message(message.msgid()).and_then(|msg| msg.value()).unwrap(); + let mut errors = vec![]; + self.message_bundles[0].format_pattern(msg, message.args().as_ref(), &mut errors).to_string() } pub fn format_date_time_utc(