Skip to content

Commit

Permalink
Merge pull request #194 from Baptistemontan/databake_currency_formatter
Browse files Browse the repository at this point in the history
Datagen for currency formatter
  • Loading branch information
Baptistemontan authored Feb 14, 2025
2 parents 02da1c6 + 68318a7 commit db2f34c
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 11 deletions.
52 changes: 51 additions & 1 deletion docs/book/src/reduce_size/01_datagen.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ pub struct MyDataProvider;
impl_data_provider!(MyDataProvider);
```

This is explained in the `icu_datagen` doc.
you will also need some depedencies:

```toml
[dependencies]
# "default-features = false" to turn off compiled_data
icu = { version = "1.5", default-features = false }
icu_provider = "1.5" # for databake
zerovec = "0.10" # for databake
```

This is explained more in depth in the `icu_datagen` doc.

## 3. Supply to leptos_i18n the provider.

Expand Down Expand Up @@ -174,6 +184,46 @@ translations_infos.generate_data_with_data_keys(mod_directory, keys).unwrap();
YES. With `opt-level = "z"` and `lto = true`, the plurals example is at 394 kB (at the time of writing). Now, by just providing a custom provider tailored to the used locales ("en" and "fr"), it shrinks down to 248 kB! It almost cut in half the binary size!
I highly suggest taking the time to implement this.

# Experimental features

When using experimental features, such as "format_currency", if you follow the step above you will probably have some compilation error in the `impl_data_provider!` macro.
To solve them you will need those few things:

### Enable experimental feature

Enable the "experimental" feature for `icu`:

```toml
# Cargo.toml
[depedencies]
icu = {
version = "1.5.0",
default-features = false,
features = [ "experimental"]
}
```

### Import `icu_pattern`

```toml
# Cargo.toml
[depedencies]
icu_pattern = "0.2.0" # for databake
```

### Import the `alloc` crate

The macro directly use the `alloc` crate instead of the std, so you must bring it into scope:

```rust,ignore
extern crate alloc;
include!(concat!(env!("OUT_DIR"), "/baked_data/mod.rs"));
pub struct MyDataProvider;
impl_data_provider!(MyDataProvider);
```

## Example

You can take a look at the `counter_icu_datagen` example. This is a copy of the `counter_plurals` example but with a custom provider.
18 changes: 15 additions & 3 deletions examples/csr/counter_icu_datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@ leptos_i18n = { path = "../../../leptos_i18n", default-features = false, feature
"json_files",
"csr",
"plurals",
"format_nums",
"format_list",
"format_datetime",
# experimental feature
"format_currency",
] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
wasm-bindgen = { version = "0.2" }

icu = { version = "1.5", default-features = false } # turn off compiled_data
icu_provider = "1.5" # for databake
zerovec = "0.10" # for databake
# "default-features = false" to turn off compiled_data
icu = { version = "1.5", default-features = false, features = [
# feature only needed for experimental features (e.g "format_currency")
"experimental",
] }
icu_provider = "1.5" # for databake
zerovec = "0.10" # for databake

# only needed for experimental features (e.g "format_currency")
icu_pattern = "0.2.0" # for databake

[package.metadata.leptos-i18n]
default = "en"
Expand Down
6 changes: 5 additions & 1 deletion examples/csr/counter_icu_datagen/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"click_to_change_lang": "Click to change language",
"click_count_one": "You clicked {{ count }} time",
"click_count_other": "You clicked {{ count }} times",
"click_to_inc": "Click to increment the counter"
"click_to_inc": "Click to increment the counter",
"num_formatter": "{{ num, number }}",
"currency_formatter": "{{ num, currency }}",
"date_formatter": "{{ date_var, date }}",
"list_formatter": "{{ list_var, list }}"
}
6 changes: 5 additions & 1 deletion examples/csr/counter_icu_datagen/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"click_to_change_lang": "Cliquez pour changez de langue",
"click_count_one": "Vous avez fait {{ count }} clique",
"click_count_other": "Vous avez fait {{ count }} cliques",
"click_to_inc": "Cliquez pour incrémenter le compteur"
"click_to_inc": "Cliquez pour incrémenter le compteur",
"num_formatter": "{{ num, number }}",
"currency_formatter": "{{ num, currency }}",
"date_formatter": "{{ date_var, date }}",
"list_formatter": "{{ list_var, list }}"
}
3 changes: 3 additions & 0 deletions examples/csr/counter_icu_datagen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ leptos_i18n::load_locales!();

include!(concat!(env!("OUT_DIR"), "/baked_data/mod.rs"));

// only needed for experimental features (e.g "format_currency")
extern crate alloc;

#[derive(leptos_i18n::custom_provider::IcuDataProvider)]
pub struct BakedProvider;
impl_data_provider!(BakedProvider);
Expand Down
5 changes: 4 additions & 1 deletion leptos_i18n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ leptos = { workspace = true }
leptos_meta = { workspace = true }
codee = "0.2"
icu_locid = { workspace = true }
icu_provider = { workspace = true, optional = true, features = ["sync"] }
icu_provider = { workspace = true, optional = true, features = [
"sync",
"experimental",
] }
icu_plurals = { workspace = true, optional = true }
icu_datetime = { workspace = true, optional = true }
icu_calendar = { workspace = true, optional = true }
Expand Down
1 change: 1 addition & 0 deletions leptos_i18n/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub mod reexports {
feature = "format_nums",
feature = "format_datetime",
feature = "format_list",
feature = "format_currency",
feature = "plurals"
))]
pub use icu_provider as provider;
Expand Down
2 changes: 1 addition & 1 deletion leptos_i18n_build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "../README.md"

[dependencies]
leptos_i18n_parser = { workspace = true }
icu_datagen = { workspace = true }
icu_datagen = { workspace = true, features = ["experimental_components"] }
icu_provider = { workspace = true }
icu_locid = { workspace = true }

Expand Down
8 changes: 5 additions & 3 deletions leptos_i18n_build/src/datakey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ impl Options {
]),
Options::FormatList => icu_datagen::keys(&["list/and@1", "list/or@1", "list/unit@1"]),
Options::FormatNums => icu_datagen::keys(&["decimal/symbols@1"]),
Options::FormatCurrency => {
icu_datagen::keys(&["decimal/symbols@1", "currency/essentials@1"])
}
Options::FormatCurrency => icu_datagen::keys(&[
"decimal/digits@1",
"decimal/symbols@2",
"currency/essentials@1",
]),
}
}
}
16 changes: 16 additions & 0 deletions leptos_i18n_macro/src/data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ pub fn derive_icu_data_provider(input: proc_macro::TokenStream) -> proc_macro::T
quote!()
};

let new_currency_formatter = if cfg!(feature = "format_currency") {
quote! {
fn try_new_currency_formatter(
&self,
locale: &leptos_i18n::reexports::icu::provider::DataLocale,
options: leptos_i18n::reexports::icu::currency::options::CurrencyFormatterOptions
) -> Result<leptos_i18n::reexports::icu::currency::formatter::CurrencyFormatter, leptos_i18n::reexports::icu::provider::DataError> {
leptos_i18n::reexports::icu::currency::formatter::CurrencyFormatter::try_new_unstable(self, locale, options)
}
}
} else {
quote!()
};

let expanded = quote! {
impl #impl_generics leptos_i18n::custom_provider::IcuDataProvider for #name #ty_generics #where_clause {

Expand All @@ -113,6 +127,8 @@ pub fn derive_icu_data_provider(input: proc_macro::TokenStream) -> proc_macro::T
#new_list_formatter

#new_plural_rules

#new_currency_formatter
}
};

Expand Down

0 comments on commit db2f34c

Please sign in to comment.