16 releases (9 stable)

1.5.0 May 28, 2024
1.4.0 Nov 16, 2023
1.3.2 Oct 3, 2023
1.2.0 Apr 14, 2023
0.1.0 Oct 15, 2020

#53 in Internationalization (i18n)

Download history 91695/week @ 2024-07-21 87685/week @ 2024-07-28 102984/week @ 2024-08-04 98723/week @ 2024-08-11 92458/week @ 2024-08-18 100865/week @ 2024-08-25 102241/week @ 2024-09-01 104993/week @ 2024-09-08 108052/week @ 2024-09-15 156614/week @ 2024-09-22 153762/week @ 2024-09-29 164479/week @ 2024-10-06 151403/week @ 2024-10-13 146224/week @ 2024-10-20 130021/week @ 2024-10-27 635656/week @ 2024-11-03

1,073,054 downloads per month
Used in 27,682 crates (47 directly)

Unicode-3.0

1MB
17K SLoC

icu_provider crates.io

icu_provider is one of the ICU4X components.

Unicode's experience with ICU4X's parent projects, ICU4C and ICU4J, led the team to realize that data management is the most critical aspect of deploying internationalization, and that it requires a high level of customization for the needs of the platform it is embedded in. As a result ICU4X comes with a selection of providers that should allow for ICU4X to naturally fit into different business and technological needs of customers.

icu_provider defines traits and structs for transmitting data through the ICU4X locale data pipeline. The primary trait is DataProvider. It is parameterized by a KeyedDataMarker, which contains the data type and a DataKey. It has one method, DataProvider::load, which transforms a DataRequest into a DataResponse.

  • DataKey is a fixed identifier for the data type, such as "plurals/cardinal@1".
  • DataRequest contains additional annotations to choose a specific variant of the key, such as a locale.
  • DataResponse contains the data if the request was successful.

In addition, there are three other traits which are widely implemented:

The most common types required for this crate are included via the prelude:

use icu_provider::prelude::*;

Types of Data Providers

All nontrivial data providers can fit into one of two classes.

  1. AnyProvider: Those whose data originates as structured Rust objects
  2. BufferProvider: Those whose data originates as unstructured [u8] buffers

✨ Key Insight: A given data provider is generally either an AnyProvider or a BufferProvider. Which type depends on the data source, and it is not generally possible to convert one to the other.

See also crate::constructors.

AnyProvider

These providers are able to return structured data cast into dyn Any trait objects. Users can call as_downcasting() to get an object implementing DataProvider by downcasting the trait objects.

Examples of AnyProviders:

  • DatagenProvider reads structured data from CLDR source files and returns ICU4X data structs.
  • AnyPayloadProvider wraps a specific data struct and returns it.
  • The BakedDataProvider which encodes structured data directly in Rust source

BufferProvider

These providers are able to return unstructured data typically represented as serde-serialized buffers. Users can call as_deserializing() to get an object implementing DataProvider by invoking Serde Deserialize.

Examples of BufferProviders:

Provider Adapters

ICU4X offers several built-in modules to combine providers in interesting ways. These can be found in the icu_provider_adapters crate.

Testing Provider

This crate also contains a concrete provider for demonstration purposes:

Types and Lifetimes

Types compatible with Yokeable can be passed through the data provider, so long as they are associated with a marker type implementing DataMarker.

Data structs should generally have one lifetime argument: 'data. This lifetime allows data structs to borrow zero-copy data.

Data generation API

This functionality is enabled with the "datagen" Cargo feature

The datagen module contains several APIs for data generation. See icu_datagen for the reference data generation implementation.

More Information

For more information on development, authorship, contributing etc. please visit ICU4X home page.

Dependencies

~0.5–1.4MB
~30K SLoC