Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to use ICU4X #335

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ rust-version = "1.67.0"

[workspace.dependencies]
criterion = "0.5"
fluent-langneg = "0.13"
fluent-langneg = "0.14"
futures = "0.3"
iai = "0.1"
intl_pluralrules = "7.0.1"
rustc-hash = "1"
serde = "1.0"
serde_json = "1.0"
thiserror = "1.0"
tokio = "1.0"
unic-langid = "0.9"
icu_locid = "1.4"
icu_plurals = { version = "1.4", features = ["experimental"] }
icu_provider = "1.4"

fluent-bundle = { version = "0.15.3", path = "fluent-bundle" }
fluent-fallback = { version = "0.7.1", path = "fluent-fallback" }
Expand Down
8 changes: 5 additions & 3 deletions fluent-bundle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ include = [
[dependencies]
fluent-langneg.workspace = true
fluent-syntax.workspace = true
intl_pluralrules.workspace = true
rustc-hash.workspace = true
unic-langid.workspace = true
icu_locid.workspace = true
icu_plurals.workspace = true
icu_provider.workspace = true
fixed_decimal = { version = "0.5.5", features = ["ryu"] }
intl-memoizer = { version = "0.5.2", path = "../intl-memoizer" }
self_cell = "1.0"
smallvec = "1.13"
Expand All @@ -37,12 +39,12 @@ smallvec = "1.13"
criterion.workspace = true
iai.workspace = true
serde = { workspace = true, features = ["derive"] }
unic-langid = { workspace = true, features = ["macros"] }
rand = "0.8"
serde_yaml = "0.9"

[features]
default = []
sync = ["intl-memoizer/sync", "icu_provider/sync"]
all-benchmarks = []

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Usage

```rust
use fluent_bundle::{FluentBundle, FluentResource};
use unic_langid::langid;
use icu_locid::langid;

fn main() {
let ftl_string = "hello-world = Hello, world!".to_owned();
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/benches/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::rc::Rc;

use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
use fluent_syntax::ast;
use unic_langid::langid;
use icu_locid::langid;

fn read_file(path: &str) -> Result<String, io::Error> {
let mut f = File::open(path)?;
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/benches/resolver_iai.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
use fluent_syntax::ast;
use unic_langid::{langid, LanguageIdentifier};
use icu_locid::{langid, LanguageIdentifier};

const LANG_EN: LanguageIdentifier = langid!("en");

Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/examples/custom_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// to format selected types of values.
//
// This allows users to plug their own number formatter to Fluent.
use unic_langid::LanguageIdentifier;
use icu_locid::LanguageIdentifier;

use fluent_bundle::memoizer::MemoizerKind;
use fluent_bundle::types::{FluentNumber, FluentNumberOptions};
Expand Down
4 changes: 3 additions & 1 deletion fluent-bundle/examples/custom_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// Lastly, we'll also create a new formatter which will be memoizable.
//
// The type and its options are modelled after ECMA402 Intl.DateTimeFormat.
use icu_locid::LanguageIdentifier;
use intl_memoizer::Memoizable;
use unic_langid::LanguageIdentifier;

use fluent_bundle::types::FluentType;
use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
Expand Down Expand Up @@ -107,6 +107,8 @@ impl FluentType for DateTime {
})
.expect("Failed to format a date.")
}

#[cfg(feature = "sync")]
fn as_string_threadsafe(
&self,
_: &intl_memoizer::concurrent::IntlLangMemoizer,
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/examples/external_arguments.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
use unic_langid::langid;
use icu_locid::langid;

fn main() {
let ftl_string = String::from(
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/examples/functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fluent_bundle::{FluentBundle, FluentResource, FluentValue};
use unic_langid::langid;
use icu_locid::langid;

fn main() {
// We define the resources here so that they outlive
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/examples/simple-app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
//! default one.
use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
use fluent_langneg::{negotiate_languages, NegotiationStrategy};
use icu_locid::{langid, LanguageIdentifier};
use std::env;
use std::fs;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;
use std::str::FromStr;
use unic_langid::{langid, LanguageIdentifier};

/// We need a generic file read helper function to
/// read the localization resource file.
Expand Down
32 changes: 22 additions & 10 deletions fluent-bundle/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::default::Default;
use std::fmt;

use fluent_syntax::ast;
use icu_locid::LanguageIdentifier;
use intl_memoizer::IntlLangMemoizer;
use unic_langid::LanguageIdentifier;

use crate::args::FluentArgs;
use crate::entry::Entry;
Expand All @@ -32,7 +32,7 @@ use crate::types::FluentValue;
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource, FluentValue, FluentArgs};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// // 1. Create a FluentResource
///
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<R, M> FluentBundle<R, M> {
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let ftl_string = String::from("
/// hello = Hi!
Expand Down Expand Up @@ -253,7 +253,7 @@ impl<R, M> FluentBundle<R, M> {
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let ftl_string = String::from("
/// hello = Hi!
Expand Down Expand Up @@ -359,7 +359,7 @@ impl<R, M> FluentBundle<R, M> {
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let ftl_string = String::from("hello = Hi!");
/// let resource = FluentResource::try_new(ftl_string)
Expand All @@ -384,7 +384,7 @@ impl<R, M> FluentBundle<R, M> {
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let ftl_string = String::from("hello-world = Hello World!");
/// let resource = FluentResource::try_new(ftl_string)
Expand Down Expand Up @@ -412,7 +412,7 @@ impl<R, M> FluentBundle<R, M> {
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let ftl_string = String::from("hello-world = Hello World!");
/// let resource = FluentResource::try_new(ftl_string)
Expand Down Expand Up @@ -459,7 +459,7 @@ impl<R, M> FluentBundle<R, M> {
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let ftl_string = String::from("hello-world = Hello World!");
/// let resource = FluentResource::try_new(ftl_string)
Expand Down Expand Up @@ -508,7 +508,7 @@ impl<R, M> FluentBundle<R, M> {
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource, FluentValue};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let ftl_string = String::from("length = { STRLEN(\"12345\") }");
/// let resource = FluentResource::try_new(ftl_string)
Expand Down Expand Up @@ -622,7 +622,7 @@ impl<R> FluentBundle<R, IntlLangMemoizer> {
/// ```
/// use fluent_bundle::FluentBundle;
/// use fluent_bundle::FluentResource;
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let langid_en = langid!("en-US");
/// let mut bundle: FluentBundle<FluentResource> = FluentBundle::new(vec![langid_en]);
Expand Down Expand Up @@ -653,6 +653,7 @@ impl crate::memoizer::MemoizerKind for IntlLangMemoizer {
Self::new(lang)
}

#[cfg(feature = "sync")]
fn with_try_get_threadsafe<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
where
Self: Sized,
Expand All @@ -663,6 +664,17 @@ impl crate::memoizer::MemoizerKind for IntlLangMemoizer {
self.with_try_get(args, cb)
}

#[cfg(not(feature = "sync"))]
fn with_try_get<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
where
Self: Sized,
I: intl_memoizer::Memoizable + 'static,
I::Args: 'static,
U: FnOnce(&I) -> R,
{
self.with_try_get(args, cb)
}

fn stringify_value(
&self,
value: &dyn crate::types::FluentType,
Expand Down
24 changes: 20 additions & 4 deletions fluent-bundle/src/concurrent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use intl_memoizer::{concurrent::IntlLangMemoizer, Memoizable};
use icu_locid::LanguageIdentifier;
#[cfg(feature = "sync")]
use intl_memoizer::concurrent::IntlLangMemoizer;
#[cfg(not(feature = "sync"))]
use intl_memoizer::IntlLangMemoizer;
use intl_memoizer::Memoizable;
use rustc_hash::FxHashMap;
use unic_langid::LanguageIdentifier;

use crate::memoizer::MemoizerKind;
use crate::types::FluentType;
Expand All @@ -23,7 +27,7 @@ impl<R> FluentBundle<R> {
/// ```
/// use fluent_bundle::concurrent::FluentBundle;
/// use fluent_bundle::FluentResource;
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let langid_en = langid!("en-US");
/// let mut bundle: FluentBundle<FluentResource> =
Expand Down Expand Up @@ -51,14 +55,26 @@ impl MemoizerKind for IntlLangMemoizer {
Self::new(lang)
}

#[cfg(feature = "sync")]
fn with_try_get_threadsafe<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
where
Self: Sized,
I: Memoizable + Send + Sync + 'static,
I::Args: Send + Sync + 'static,
U: FnOnce(&I) -> R,
{
self.with_try_get(args, cb)
Self::with_try_get(self, args, cb)
}

#[cfg(not(feature = "sync"))]
fn with_try_get<I, R, U>(&self, args: I::Args, cb: U) -> Result<R, I::Error>
where
Self: Sized,
I: Memoizable + 'static,
I::Args: 'static,
U: FnOnce(&I) -> R,
{
Self::with_try_get(self, args, cb)
}

fn stringify_value(&self, value: &dyn FluentType) -> std::borrow::Cow<'static, str> {
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum FluentError {
///
/// ```
/// use fluent_bundle::{FluentBundle, FluentResource};
/// use unic_langid::langid;
/// use icu_locid::langid;
///
/// let ftl_string = String::from("intro = Welcome, { $name }.");
/// let res1 = FluentResource::try_new(ftl_string)
Expand Down
3 changes: 2 additions & 1 deletion fluent-bundle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! ```
//! use fluent_bundle::{FluentBundle, FluentValue, FluentResource, FluentArgs};
//! // Used to provide a locale for the bundle.
//! use unic_langid::langid;
//! use icu_locid::langid;
//!
//! // 1. Crate a FluentResource
//!
Expand Down Expand Up @@ -101,6 +101,7 @@
mod args;
pub mod builtins;
pub mod bundle;
#[cfg(feature = "sync")]
pub mod concurrent;
mod entry;
mod errors;
Expand Down
11 changes: 10 additions & 1 deletion fluent-bundle/src/memoizer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::types::FluentType;
use icu_locid::LanguageIdentifier;
use intl_memoizer::Memoizable;
use unic_langid::LanguageIdentifier;

/// This trait contains thread-safe methods which extend [`intl_memoizer::IntlLangMemoizer`].
/// It is used as the generic bound in this crate when a memoizer is needed.
Expand All @@ -18,13 +18,22 @@ pub trait MemoizerKind: 'static {
///
/// `U` - The callback that accepts the instance of the intl formatter, and generates
/// some kind of results `R`.
#[cfg(feature = "sync")]
fn with_try_get_threadsafe<I, R, U>(&self, args: I::Args, callback: U) -> Result<R, I::Error>
where
Self: Sized,
I: Memoizable + Send + Sync + 'static,
I::Args: Send + Sync + 'static,
U: FnOnce(&I) -> R;

#[cfg(not(feature = "sync"))]
fn with_try_get<I, R, U>(&self, args: I::Args, callback: U) -> Result<R, I::Error>
where
Self: Sized,
I: Memoizable + 'static,
I::Args: 'static,
U: FnOnce(&I) -> R;

/// Wires up the `as_string` or `as_string_threadsafe` variants for [`FluentType`].
fn stringify_value(&self, value: &dyn FluentType) -> std::borrow::Cow<'static, str>;
}
Loading
Loading