Fuchsia uses the ICU library for the commonly shared internationalization services such as date, time, timezone, locale and language handling.
The ICU library consists roughly of two different parts: the ICU library code which contains the ICU algorithms, and the ICU library data, which contains locale-specific information that is packaged for independent reuse.
Add ICU library dependencies
Include the necessary library dependencies in your BUILD.gn
file:
C++
The ICU library is a single third-party dependency //third_party/icu
:
"//third_party/icu:icu",
Rust
The rust_icu
library is subdivided into several crates that correspond to
specific ICU4C headers:
"//third_party/rust_crates:rust_icu_common",
"//third_party/rust_crates:rust_icu_sys",
"//third_party/rust_crates:rust_icu_ucal",
"//third_party/rust_crates:rust_icu_udat",
"//third_party/rust_crates:rust_icu_udata",
"//third_party/rust_crates:rust_icu_uenum",
"//third_party/rust_crates:rust_icu_uloc",
"//third_party/rust_crates:rust_icu_ustring",
Import ICU headers
Add the imports for the specific ICU library features your program requires:
C++
The ICU documentation provides additional information about the APIs the library supports.
#include "third_party/icu/source/common/unicode/locid.h"
#include "third_party/icu/source/common/unicode/unistr.h"
#include "third_party/icu/source/i18n/unicode/calendar.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
#include "third_party/icu/source/i18n/unicode/tzfmt.h"
Rust
The rust_icu reference provides additional information about the APIs the library supports.
use {
anyhow::Error, fidl_fuchsia_examples_intl_wisdom as fwisdom, fidl_fuchsia_intl as fintl,
rust_icu_sys as usys, rust_icu_ucal as ucal, rust_icu_udat as udat, rust_icu_uloc as uloc,
rust_icu_ustring as ustring, std::convert::TryFrom,
};
Initialize ICU data
In Fuchsia, the ICU data must be loaded by the program at runtime. Initialize
the ICU data with the icu_data
library:
C++
ZX_ASSERT(icu_data::Initialize() == ZX_OK);
Rust
// Force the loading of ICU data at the beginning of the program. Since
// Fuchsia's ICU does not have `libicudata.so`, we must load the data here
// so that locales could be used in the server.
let _loader = icu_data::Loader::new()?;
For more details instructions on loading ICU data from various sources, see ICU timezone data.
You are now ready to use the ICU library features in your Fuchsia program.