Crate i18n_utility

source ·
Expand description

Welcome to the i18n_utility crate of the Internationalisation (i18n) project.

This crate consists of five modules:

§Features

Available features for i18n_utility crate:

  • icu_extended: Use the more detailed ICU information structs, types, and methods.

  • sync: Allow for rust’s concurrency capabilities to be used. Use of Arc and Mutex instead Rc and RefCell.

§Modules

§language: Registry for holding ICU4X Locale objects.

This module contains the LanguageTagRegistry type, to provide a simple container that caches the BCP 47 Language Tag strings and the LanguageIdentifier instances, or when the icu_extended feature is enabled the Locale instances is used instead.

The purpose of the registry is to reduce the need of parsing language tags repeatedly, by storing the result LanguageIdentifier for querying language tag in the registry, and uses the existing LanguageIdentifier for the querying language tag when requested.

The LanguageIdentifier or Locale type can be provided by either the icu_locid crate or the icu meta-crate. These two crates are part of the ICU4X project developed by the Unicode Consortium.

§Examples

use icu_locid::LanguageIdentifier;
use std::rc::Rc;
use i18n_utility::LanguageTagRegistry;

let mut registry = LanguageTagRegistry::new();
let result = registry.tag_and_identifier("en_ZA").expect("Failed to parse language tag.");
let tags = registry.list().iter().count();

assert_eq!(result.0.as_str(), "en-ZA", "Did not convert en_ZA to en-ZA BCP 47 format.");
assert_eq!(tags, 1, "Supposed to be 1 entries: en-ZA.")

§tagged_string: Tagged string.

The immutable TaggedString type simply associates a language tag (Rc<LanguageTag> or Arc<LanguageTag>) to a text string (String).

In the context of the i18n project, the identifier tag is expected to be a BCP 47 Language Tag string.

§Examples

use i18n_utility::{TaggedString, LanguageTagRegistry};

let registry = LanguageTagRegistry::new();
let string = "This is a test string.";
let tag = registry.tag("en-ZA").expect("Failed to canonicalise language tag.");
let tagged_string = TaggedString::new(string, &tag);
assert_eq!(tagged_string.as_str(), string, "String failed.");
assert_eq!(tagged_string.tag(), &tag, "Language tag failed.");

Re-exports§

Modules§