Skip to main content

Module runtime_api

Module runtime_api 

Source
Expand description

set_locale, Locale, locale!, available_locales!, system_locale and is_right_to_left.

§Runtime API

The items to select and read the locale at runtime. They are useful when several locales are embedded (available_locales, see Configuration), and build with a single locale too.

ItemKindDescription
set_locale(&str)functionSets the global current locale
LocalestructCopy handle of a locale tag, packed into a u32
locale!()macroName of the locale the calling crate translates into, a &'static str
available_locales!()macroEmbedded locales of the calling crate, a &'static [&'static str]
system_locale()function, std featureLocale of the operating system, an Option<String>
is_right_to_left(&str)const fnWhether a locale is written right to left
t!("key", locale = ...)t! optionTranslates into a given locale, see The t! Macro

§set_locale

pub fn set_locale(locale: &str)
  • Stores the Locale identifier of the tag in a global AtomicU32, with a plain store: no compare-and-swap, no allocation, no lock.
  • Returns (), as in rust-i18n, and never fails: each crate translates into its closest embedded locale, see Resolution.
  • An empty tag restores the unset state, in which every crate uses its default_locale.
  • The current locale is shared by every crate of the program.
  • With a single embedded locale, it has no effect on t!.

§Locale

use embedded_i18n::Locale;

const FRENCH_CANADA: Locale = Locale::new("fr-CA");

assert_eq!(Locale::new("fr_CA"), Locale::new("FR-ca"));
assert_eq!(Locale::from("fr-CA"), FRENCH_CANADA);
assert_eq!(Locale::new("en").id(), 0x015C_0000);
assert_eq!(Locale::NONE.id(), 0);
ItemDescription
Locale::new(&str)const fn packing a tag into its identifier
From<&str>the same, for Into<Locale> arguments
.id()the u32 identifier
Locale::NONEidentifier 0, the empty tag: no locale set
Clone, Copy, Debug, PartialEq, Eq, Hashderived

Tags are normalized before packing: surrounding ASCII whitespace trimmed, ASCII case ignored, _ equivalent to -. The layout of the identifier:

BitsContent
31 to 17The language subtag, when it is 1 to 3 ASCII letters, 5 bits per letter (a is 1), 0 for none
16 to 0A hash of the rest of the tag (script, region, variants), 0 when there is none and never 0 otherwise

A tag that does not start with a 1 to 3 letter language subtag (pseudo) uses the language value 0x7FFF and hashes the whole tag into the low bits: it only matches exactly. See The Runtime Locale Model for the reasons and the limitations.

§locale!

Returns the name of the locale the calling crate translates into, as a &'static str.

  • With a single embedded locale, it is the default_locale literal, usable in const items.
  • With several, it is the embedded locale the current locale resolves to, which can differ from the tag given to set_locale (fr-CA can resolve to fr).
embedded_i18n::i18n!();

assert_eq!(embedded_i18n::locale!(), "en");

§available_locales!

Returns the locales embedded in the calling crate, as a &'static [&'static str], in configuration order. With a single locale, it is [default_locale].

embedded_i18n::i18n!();

assert_eq!(embedded_i18n::available_locales!(), ["en"]);

§system_locale

#[cfg(feature = "std")]
pub fn system_locale() -> Option<String>
  • Reads LC_ALL, LC_MESSAGES, then LANG; the first variable holding a usable value wins.
  • Skips empty, C and POSIX values (and C.UTF-8).
  • Normalizes the value to a tag: fr_FR.UTF-8@euro becomes fr-FR.
  • Returns None when no variable is usable.
  • Allocates a String on each call.
  • Reads the POSIX environment only: Windows and macOS graphical applications should use the sys-locale crate instead.

§is_right_to_left

A const fn returning whether a locale is written right to left. It recognizes, case-insensitively and with - or _ separators, the right-to-left languages ar, arc, ckb, dv, fa, he, iw, ks, ps, sd, syr, ug, ur and yi, and the Arab and Hebr script subtags (pa_Arab).

use embedded_i18n::is_right_to_left;

assert!(is_right_to_left("ar-EG"));
assert!(is_right_to_left("pa_Arab"));
assert!(!is_right_to_left("fr-CA"));

§Resolution

With several embedded locales, each crate maps the current locale, or the locale = ... of a t! call, to one of its embedded locales, in this order:

  1. an exact identifier match;
  2. the same language: the locale that is the language alone (fr) if embedded, otherwise the first embedded locale of that language (fr-CA);
  3. the default_locale.

The unset state (Locale::NONE) resolves to the default_locale. A literal locale = "fr" is resolved the same way, at compile time.

use embedded_i18n::{set_locale, t};

embedded_i18n::i18n!("locales", default_locale = "en", available_locales = ["en", "fr"]);

fn main() {
    set_locale("fr-CA");
    assert_eq!(embedded_i18n::locale!(), "fr");
    assert_eq!(t!("greeting"), "Bonjour");

    set_locale("de");
    assert_eq!(embedded_i18n::locale!(), "en");

    set_locale("");
    assert_eq!(embedded_i18n::locale!(), "en");
}

§Limits

  • Up to 255 locales can be embedded in a crate.
  • Two embedded locales with the same identifier are a compile error on i18n!.
  • With several embedded locales, t! returns runtime values, which cannot initialize const or static items.