International Components for Unicode (ICU) use in Fuchsia

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, used as ${icu_root}:icu

third_party_icu_headers("icu_headers") {
  headers = [
    "third_party/icu/source/common/unicode/locid.h",
    "third_party/icu/source/common/unicode/ucnv.h",
    "third_party/icu/source/common/unicode/unistr.h",
    "third_party/icu/source/i18n/unicode/calendar.h",
    "third_party/icu/source/i18n/unicode/gregocal.h",
    "third_party/icu/source/i18n/unicode/smpdtfmt.h",
    "third_party/icu/source/i18n/unicode/timezone.h",
    "third_party/icu/source/i18n/unicode/tzfmt.h",
  ]
}

source_set("lib") {
  sources = [
    "intl_wisdom_client.cc",
    "intl_wisdom_client.h",
  ]

  public_deps = [
    ":icu_headers",
    "//examples/intl/wisdom/fidl:wisdom_hlcpp",
    "//sdk/lib/fidl/cpp",
    "//sdk/lib/sys/cpp",
  ]
}

Rust

The rust_icu library is subdivided into several crates that correspond to specific ICU4C headers:

"//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_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 "examples/intl/wisdom/cpp/client/icu_headers.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.