ICU4X makes heavy use of small crates and Cargo features in order to be highly modular. This tutorial is intended to help you set up a Cargo.toml file to download what you need for ICU4X.
The most basic Cargo.toml to get you off the ground is the following:
[dependencies]
icu = "2.0.0-beta1"
In your main.rs, you can use all stable ICU4X components for the recommended set of locales, which get compiled into the library.
If you wish to use custom compiled data for ICU4X, no changes to Cargo.toml are required. Instead, set the ICU4X_DATA_DIR
environment variable to the
datagen output during your build:
icu4x-datagen --format baked --markers all --locales ru --out baked_data
ICU4X_DATA_DIR=$(pwd)/baked_data cargo build --release
Experimental modules are published in a separate icu_experimental
crate:
[dependencies]
icu = { version = "2.0.0-beta1", features = ["experimental"] }
In your main.rs, you can now use e.g. the icu_experimental::displaynames
module.
If you wish to generate your own data in blob format and pass it into ICU4X, enable the "serde" Cargo feature as follows:
[dependencies]
icu = { version = "2.0.0-beta1", features = ["serde"] }
icu_provider_blob = "2.0.0-beta1"
To learn about building ICU4X data, including whether to check in the data blob file to your repository, see data_management.md.
If you wish to share ICU4X objects between threads, you must enable the "sync"
Cargo feature:
[dependencies]
icu = { version = "2.0.0-beta1", features = ["sync"] }
You can now use most ICU4X types when Send + Sync
are required, such as when sharing across threads.
If you wish to use data generation in a build.rs
script, you need to manually include the data and any dependencies (the ICU4X_DATA_DIR
variable won't work as ICU4X cannot access your build script output).
[dependencies]
icu = { version = "2.0.0-beta1", default-features = false } # turn off compiled_data
icu_provider = "2.0.0-beta1" # for databake
icu_provider_baked = "2.0.0-beta1" # for databake
zerovec = "0.9" # for databake
# for build.rs:
[build-dependencies]
icu = "2.0.0-beta1"
icu_provider_export = "2.0.0-beta1"
icu_provider_source = "2.0.0-beta1"
This example has an additional section for auto-generating the data in build.rs. In your build.rs, invoke the ICU4X Datagen API with the set of markers you require. Don't worry; if using databake, you will get a compiler error if you don't specify enough markers.
The build.rs approach has several downsides and should only be used if Cargo is the only build system you can use, and you cannot check in your data:
- The build script with the whole of
icu_provider_source
in it is slow to build - If you're using networking features of
icu_provider_source
(behind thenetworking
Cargo feature), the build script will access the network - Using the data requires ICU4X's
_unstable
APIs with a custom data provider, and thaticu_provider_source
is the same minor version as theicu
crate. build.rs
output is not written to the console so it will appear that the build is hanging