From 679510b01076473ac3f3f4d96fa87e6237db3105 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Mon, 7 Aug 2023 18:01:27 -0400 Subject: [PATCH] Add async-safety back to fluent-ergonomics --- Cargo.lock | 3 ++- fluent-ergonomics/Cargo.toml | 3 ++- fluent-ergonomics/src/lib.rs | 24 +++++++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 144a971..350c091 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -561,8 +561,9 @@ dependencies = [ name = "fluent-ergonomics" version = "0.2.0" dependencies = [ - "fluent", + "fluent-bundle", "fluent-syntax", + "intl-memoizer", "unic-langid", ] diff --git a/fluent-ergonomics/Cargo.toml b/fluent-ergonomics/Cargo.toml index 3415160..edddd75 100644 --- a/fluent-ergonomics/Cargo.toml +++ b/fluent-ergonomics/Cargo.toml @@ -19,6 +19,7 @@ include = [ ] [dependencies] -fluent = "0.16" +fluent-bundle = "0.15" unic-langid = "0.9" fluent-syntax = "0.11" +intl-memoizer = "*" diff --git a/fluent-ergonomics/src/lib.rs b/fluent-ergonomics/src/lib.rs index 21a288a..d6e6bcf 100644 --- a/fluent-ergonomics/src/lib.rs +++ b/fluent-ergonomics/src/lib.rs @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with Lum //! The Fluent class makes it easier to load translation bundles with language fallbacks and to go //! through the most common steps of translating a message. //! -use fluent::{FluentArgs, FluentBundle, FluentError, FluentResource}; +use fluent_bundle::{bundle::FluentBundle, FluentArgs, FluentError, FluentResource}; use fluent_syntax::parser::ParserError; use std::collections::hash_map::Entry; use std::collections::HashMap; @@ -103,7 +103,14 @@ impl From for Error { #[derive(Clone, Default)] pub struct FluentErgo { languages: Vec, - bundles: Arc>>>, + bundles: Arc< + RwLock< + HashMap< + LanguageIdentifier, + FluentBundle, + >, + >, + >, } impl fmt::Debug for FluentErgo { @@ -167,7 +174,10 @@ impl FluentErgo { bundle.add_resource(res).map_err(|err| Error::from(err)) } Entry::Vacant(e) => { - let mut bundle = FluentBundle::new(vec![lang]); + let mut bundle: FluentBundle< + FluentResource, + intl_memoizer::concurrent::IntlLangMemoizer, + > = FluentBundle::new_concurrent(vec![lang]); bundle.add_resource(res).map_err(|err| Error::from(err))?; e.insert(bundle); Ok(()) @@ -220,14 +230,14 @@ impl FluentErgo { /// A typical call with arguments would look like this: /// /// ``` - /// use fluent::{FluentArgs, FluentValue}; + /// use fluent_bundle::{FluentArgs, FluentValue}; /// /// let eo_id = "eo".parse::().unwrap(); /// let en_id = "en-US".parse::().unwrap(); /// /// let mut fluent = fluent_ergonomics::FluentErgo::new(&[eo_id, en_id]); /// let mut args = FluentArgs::new(); - /// args.insert("value", FluentValue::from("15")); + /// args.set("value", FluentValue::from("15")); /// let r = fluent.tr("length-without-label", Some(&args)); /// ``` /// @@ -257,7 +267,7 @@ impl FluentErgo { fn tr_( &self, - bundle: &FluentBundle, + bundle: &FluentBundle, msgid: &str, args: Option<&FluentArgs>, ) -> Option { @@ -287,7 +297,7 @@ impl FluentErgo { #[cfg(test)] mod tests { use super::FluentErgo; - use fluent::{FluentArgs, FluentValue}; + use fluent_bundle::{FluentArgs, FluentValue}; use unic_langid::LanguageIdentifier; const EN_TRANSLATIONS: &'static str = "