Remove FluentErgo from the l10n stack

This commit is contained in:
Savanni D'Gerinel 2024-03-11 09:09:53 -04:00
parent 15b1d4bfd6
commit 034bda502a
3 changed files with 57 additions and 29 deletions

View File

@ -1,7 +1,6 @@
// This file was autogenerated from l10n-codegen-rust. Edits will be lost // This file was autogenerated from l10n-codegen-rust. Edits will be lost
// on next generation. // on next generation.
use l10n::Message; use l10n::Message;
use fluent_ergonomics::FluentErgo;
use fluent::FluentArgs; use fluent::FluentArgs;
pub struct Hello { pub struct Hello {
@ -9,24 +8,25 @@ pub struct Hello {
} }
impl Message for Hello { impl Message for Hello {
fn localize(&self, bundle: &FluentErgo) -> String { fn msgid(&self) -> &str {
let mut args = FluentArgs::new(); "hello"
args.set("name", self.name.clone());
bundle.tr("hello", Some(&args)).unwrap()
}
} }
pub struct NothingHere; fn args(&self) -> Option<FluentArgs> {
impl Message for NothingHere { let mut args = FluentArgs::new();
fn localize(&self, bundle: &FluentErgo) -> String { args.set("name", self.name.clone());
bundle.tr("nothing-here", None).unwrap() Some(args)
} }
} }
pub struct Welcome; pub struct Welcome;
impl Message for Welcome { impl Message for Welcome {
fn localize(&self, bundle: &FluentErgo) -> String { fn msgid(&self) -> &str {
bundle.tr("welcome", None).unwrap() "welcome"
}
fn args(&self) -> Option<FluentArgs> {
None
} }
} }
@ -35,9 +35,24 @@ pub struct GamesInDatabase {
} }
impl Message for GamesInDatabase { impl Message for GamesInDatabase {
fn localize(&self, bundle: &FluentErgo) -> String { fn msgid(&self) -> &str {
"games-in-database"
}
fn args(&self) -> Option<FluentArgs> {
let mut args = FluentArgs::new(); let mut args = FluentArgs::new();
args.set("count", self.count.clone()); 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<FluentArgs> {
None
} }
} }

View File

@ -81,8 +81,12 @@ impl Message {
struct_strs.push(format!("pub struct {}; ", self.name.to_case(Case::Pascal))); struct_strs.push(format!("pub struct {}; ", self.name.to_case(Case::Pascal)));
struct_strs.push(format!("impl Message for {} {{ struct_strs.push(format!("impl Message for {} {{
fn localize(&self, bundle: &FluentErgo) -> String {{ fn msgid(&self) -> &str {{
bundle.tr(\"{}\", None).unwrap() \"{}\"
}}
fn args(&self) -> Option<FluentArgs> {{
None
}} }}
}}", }}",
self.name.to_case(Case::Pascal), self.name.to_case(Case::Pascal),
@ -114,15 +118,19 @@ impl Message {
struct_strs.push(format!( struct_strs.push(format!(
"impl Message for {} {{ "impl Message for {} {{
fn localize(&self, bundle: &FluentErgo) -> String {{ fn msgid(&self) -> &str {{
\"{}\"
}}
fn args(&self) -> Option<FluentArgs> {{
let mut args = FluentArgs::new(); let mut args = FluentArgs::new();
{} {}
bundle.tr(\"{}\", Some(&args)).unwrap() Some(args)
}} }}
}}", }}",
self.name.to_case(Case::Pascal), self.name.to_case(Case::Pascal),
parameters,
self.name, self.name,
parameters,
)); ));
struct_strs.join("\n") struct_strs.join("\n")
@ -144,7 +152,6 @@ fn main() {
println!("// This file was autogenerated from l10n-codegen-rust. Edits will be lost println!("// This file was autogenerated from l10n-codegen-rust. Edits will be lost
// on next generation."); // on next generation.");
println!("use l10n::Message;"); println!("use l10n::Message;");
println!("use fluent_ergonomics::FluentErgo;");
println!("use fluent::FluentArgs;"); println!("use fluent::FluentArgs;");
let messages = messages let messages = messages

View File

@ -68,12 +68,13 @@ impl From<icu::locid::Error> for L10NError {
// However, I have not found a mechanism in Fluent to identify all of the placeholders within a // 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. // message, so I'm not even sure that I can automate this code generation.
pub trait Message { pub trait Message {
fn localize(&self, bundle: &FluentErgo) -> String; fn msgid(&self) -> &str;
fn args(&self) -> Option<FluentArgs>;
} }
pub struct L10N { pub struct L10N {
messages_root: std::path::PathBuf, messages_root: std::path::PathBuf,
message_bundle: FluentErgo, message_bundles: Vec<FluentBundle<FluentResource>>,
locales: NonEmptyList<Locale>, locales: NonEmptyList<Locale>,
zone: chrono_tz::Tz, zone: chrono_tz::Tz,
@ -94,7 +95,8 @@ impl L10N {
let english_phrases = FluentResource::try_new let english_phrases = FluentResource::try_new
*/ */
let message_bundle = { let message_bundles = {
/*
let mut english_messages = messages_root.clone(); let mut english_messages = messages_root.clone();
english_messages.push("en-US.ftl"); english_messages.push("en-US.ftl");
@ -102,12 +104,14 @@ impl L10N {
let mut messages = FluentErgo::new(&[langid.clone()]); let mut messages = FluentErgo::new(&[langid.clone()]);
let _ = messages.add_from_file(langid, &english_messages); let _ = messages.add_from_file(langid, &english_messages);
messages vec![messages]
*/
vec![]
}; };
Self { Self {
messages_root, messages_root,
message_bundle, message_bundles,
locales, locales,
zone, zone,
} }
@ -156,12 +160,14 @@ impl L10N {
// parameters. In an ideal world, neither of these can be incorrect. Messages are all checked // 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 // at compile time, as are their parameters. That implies an enumeration, with one element per
// message, and with each element knowing its parameters. // message, and with each element knowing its parameters.
pub fn messages(&self) -> FluentErgo { // pub fn messages(&self) -> Vec<FluentBundle<FluentResource>> {
self.message_bundle.clone() // self.message_bundles.clone()
} // }
pub fn tr(&self, message: impl Message) -> String { 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( pub fn format_date_time_utc(