Add async-safety back to fluent-ergonomics

This commit is contained in:
Savanni D'Gerinel 2023-08-07 18:01:27 -04:00
parent ab33db4a22
commit 679510b010
3 changed files with 21 additions and 9 deletions

3
Cargo.lock generated
View File

@ -561,8 +561,9 @@ dependencies = [
name = "fluent-ergonomics" name = "fluent-ergonomics"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"fluent", "fluent-bundle",
"fluent-syntax", "fluent-syntax",
"intl-memoizer",
"unic-langid", "unic-langid",
] ]

View File

@ -19,6 +19,7 @@ include = [
] ]
[dependencies] [dependencies]
fluent = "0.16" fluent-bundle = "0.15"
unic-langid = "0.9" unic-langid = "0.9"
fluent-syntax = "0.11" fluent-syntax = "0.11"
intl-memoizer = "*"

View File

@ -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 //! 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. //! 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 fluent_syntax::parser::ParserError;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::collections::HashMap; use std::collections::HashMap;
@ -103,7 +103,14 @@ impl From<FromUtf8Error> for Error {
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct FluentErgo { pub struct FluentErgo {
languages: Vec<LanguageIdentifier>, languages: Vec<LanguageIdentifier>,
bundles: Arc<RwLock<HashMap<LanguageIdentifier, FluentBundle<FluentResource>>>>, bundles: Arc<
RwLock<
HashMap<
LanguageIdentifier,
FluentBundle<FluentResource, intl_memoizer::concurrent::IntlLangMemoizer>,
>,
>,
>,
} }
impl fmt::Debug for FluentErgo { impl fmt::Debug for FluentErgo {
@ -167,7 +174,10 @@ impl FluentErgo {
bundle.add_resource(res).map_err(|err| Error::from(err)) bundle.add_resource(res).map_err(|err| Error::from(err))
} }
Entry::Vacant(e) => { 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))?; bundle.add_resource(res).map_err(|err| Error::from(err))?;
e.insert(bundle); e.insert(bundle);
Ok(()) Ok(())
@ -220,14 +230,14 @@ impl FluentErgo {
/// A typical call with arguments would look like this: /// A typical call with arguments would look like this:
/// ///
/// ``` /// ```
/// use fluent::{FluentArgs, FluentValue}; /// use fluent_bundle::{FluentArgs, FluentValue};
/// ///
/// let eo_id = "eo".parse::<unic_langid::LanguageIdentifier>().unwrap(); /// let eo_id = "eo".parse::<unic_langid::LanguageIdentifier>().unwrap();
/// let en_id = "en-US".parse::<unic_langid::LanguageIdentifier>().unwrap(); /// let en_id = "en-US".parse::<unic_langid::LanguageIdentifier>().unwrap();
/// ///
/// let mut fluent = fluent_ergonomics::FluentErgo::new(&[eo_id, en_id]); /// let mut fluent = fluent_ergonomics::FluentErgo::new(&[eo_id, en_id]);
/// let mut args = FluentArgs::new(); /// 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)); /// let r = fluent.tr("length-without-label", Some(&args));
/// ``` /// ```
/// ///
@ -257,7 +267,7 @@ impl FluentErgo {
fn tr_( fn tr_(
&self, &self,
bundle: &FluentBundle<FluentResource>, bundle: &FluentBundle<FluentResource, intl_memoizer::concurrent::IntlLangMemoizer>,
msgid: &str, msgid: &str,
args: Option<&FluentArgs>, args: Option<&FluentArgs>,
) -> Option<String> { ) -> Option<String> {
@ -287,7 +297,7 @@ impl FluentErgo {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::FluentErgo; use super::FluentErgo;
use fluent::{FluentArgs, FluentValue}; use fluent_bundle::{FluentArgs, FluentValue};
use unic_langid::LanguageIdentifier; use unic_langid::LanguageIdentifier;
const EN_TRANSLATIONS: &'static str = " const EN_TRANSLATIONS: &'static str = "