Skip to content

Commit

Permalink
add baked ICU4X provider to fluent-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperDeSutter committed May 18, 2024
1 parent 55f6483 commit 5812fda
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ unic-langid = "0.9"
icu_locid = "1.4"
icu_provider = "1.4"
icu_decimal = "1.4"
icu = { version = "1.4", default-features = false }
fixed_decimal = "0.5"

fluent-bundle = { version = "0.15.3", path = "fluent-bundle" }
Expand Down
2 changes: 1 addition & 1 deletion fluent-bundle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fluent-syntax.workspace = true
icu_decimal.workspace = true
icu_locid.workspace = true
icu_provider = { workspace = true, features = ["sync"] }
icu.workspace = true
intl_pluralrules.workspace = true
rustc-hash.workspace = true
unic-langid.workspace = true
Expand All @@ -40,7 +41,6 @@ smallvec = "1.13"
[dev-dependencies]
criterion.workspace = true
iai.workspace = true
icu_testdata = "1.4"
serde = { workspace = true, features = ["derive"] }
unic-langid = { workspace = true, features = ["macros"] }
rand = "0.8"
Expand Down
2 changes: 2 additions & 0 deletions fluent-bundle/src/icu4x_data/any.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @generated
impl_any_provider ! (BakedDataProvider) ;
2 changes: 2 additions & 0 deletions fluent-bundle/src/icu4x_data/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @generated
# [doc = r" Marks a type as a data provider. You can then use macros like"] # [doc = r" `impl_core_helloworld_v1` to add implementations."] # [doc = r""] # [doc = r" ```ignore"] # [doc = r" struct MyProvider;"] # [doc = r" const _: () = {"] # [doc = r#" include!("path/to/generated/macros.rs");"#] # [doc = r" make_provider!(MyProvider);"] # [doc = r" impl_core_helloworld_v1!(MyProvider);"] # [doc = r" }"] # [doc = r" ```"] # [doc (hidden)] # [macro_export] macro_rules ! __make_provider { ($ name : ty) => { # [clippy :: msrv = "1.67"] impl $ name { # [doc (hidden)] # [allow (dead_code)] pub const MUST_USE_MAKE_PROVIDER_MACRO : () = () ; } } ; } # [doc (inline)] pub use __make_provider as make_provider ; # [macro_use] # [path = "macros/decimal_symbols_v1.rs.data"] mod decimal_symbols_v1 ; # [doc (inline)] pub use __impl_decimal_symbols_v1 as impl_decimal_symbols_v1 ;

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions fluent-bundle/src/icu4x_data/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @generated
include ! ("macros.rs") ; macro_rules ! impl_data_provider { ($ provider : ty) => { make_provider ! ($ provider) ; impl_decimal_symbols_v1 ! ($ provider) ; } ; } # [allow (unused_macros)] macro_rules ! impl_any_provider { ($ provider : ty) => { # [clippy :: msrv = "1.67"] impl icu_provider :: AnyProvider for $ provider { fn load_any (& self , key : icu_provider :: DataKey , req : icu_provider :: DataRequest) -> Result < icu_provider :: AnyResponse , icu_provider :: DataError > { match key . hashed () { h if h == < icu::decimal :: provider :: DecimalSymbolsV1Marker as icu_provider :: KeyedDataMarker > :: KEY . hashed () => icu_provider :: DataProvider :: < icu::decimal :: provider :: DecimalSymbolsV1Marker > :: load (self , req) . map (icu_provider :: DataResponse :: wrap_into_any_response) , _ => Err (icu_provider :: DataErrorKind :: MissingDataKey . with_req (key , req)) , } } } } } # [clippy :: msrv = "1.67"] pub struct BakedDataProvider ; impl_data_provider ! (BakedDataProvider) ;
26 changes: 26 additions & 0 deletions fluent-bundle/src/icu_data_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate alloc;

// to regerate the icu4x_data module, run from the root of the project:
// cargo build --examples
// rm -r fluent-bundle/src/icu4x_data
// icu4x-datagen --keys-for-bin target/debug/examples/functions --format mod --locales full -o fluent-bundle/src/icu4x_data
// for more information: https://github.com/unicode-org/icu4x/blob/79480dfbafdedf6cc810e4bfb0770f3268ff86ab/tutorials/data_management.md
include!("./icu4x_data/mod.rs");
impl_any_provider!(BakedDataProvider);

/// ICU data provider
///
/// Fluent uses ICU4X data for formatting purposes, like number formatting.
/// Use FluentIcuDataProvider to add all ICU data needed by fluent-bundle.
/// You can bring your own provider if you don't need all locales, or have one in your project already.
///
/// Example:
/// ```
/// use fluent_bundle::{FluentBundle, FluentIcuDataProvider, FluentResource};
/// use unic_langid::langid;
///
/// let langid = langid!("en-US");
/// let mut bundle: FluentBundle<FluentResource> = FluentBundle::new(vec![langid]);
/// bundle.set_icu_data_provider(Box::new(FluentIcuDataProvider));
/// ```
pub use BakedDataProvider as FluentIcuDataProvider;
2 changes: 2 additions & 0 deletions fluent-bundle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub mod bundle;
pub mod concurrent;
mod entry;
mod errors;
mod icu_data_provider;
#[doc(hidden)]
pub mod memoizer;
mod message;
Expand All @@ -122,6 +123,7 @@ pub use args::FluentArgs;
/// [`FluentBundle::new_concurrent`](crate::concurrent::FluentBundle::new_concurrent).
pub type FluentBundle<R> = bundle::FluentBundle<R, intl_memoizer::IntlLangMemoizer>;
pub use errors::FluentError;
pub use icu_data_provider::FluentIcuDataProvider;
pub use message::{FluentAttribute, FluentMessage};
pub use resource::FluentResource;
#[doc(inline)]
Expand Down
3 changes: 1 addition & 2 deletions fluent-bundle/tests/types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ fn fluent_number_to_operands() {
fn fluent_number_grouping() {
let langid_ars = langid!("ccp");
let mut bundle: FluentBundle<FluentResource> = FluentBundle::new(vec![langid_ars]);
#[allow(deprecated)]
bundle.set_icu_data_provider(Box::new(icu_testdata::any_no_fallback()));
bundle.set_icu_data_provider(Box::new(fluent_bundle::FluentIcuDataProvider));

let mut number = FluentNumber::from(1234567890.1234567);

Expand Down

0 comments on commit 5812fda

Please sign in to comment.