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.
| Item | Kind | Description |
|---|---|---|
set_locale(&str) | function | Sets the global current locale |
Locale | struct | Copy handle of a locale tag, packed into a u32 |
locale!() | macro | Name of the locale the calling crate translates into, a &'static str |
available_locales!() | macro | Embedded locales of the calling crate, a &'static [&'static str] |
system_locale() | function, std feature | Locale of the operating system, an Option<String> |
is_right_to_left(&str) | const fn | Whether a locale is written right to left |
t!("key", locale = ...) | t! option | Translates into a given locale, see The t! Macro |
§set_locale
pub fn set_locale(locale: &str)- Stores the
Localeidentifier of the tag in a globalAtomicU32, 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);| Item | Description |
|---|---|
Locale::new(&str) | const fn packing a tag into its identifier |
From<&str> | the same, for Into<Locale> arguments |
.id() | the u32 identifier |
Locale::NONE | identifier 0, the empty tag: no locale set |
Clone, Copy, Debug, PartialEq, Eq, Hash | derived |
Tags are normalized before packing: surrounding ASCII whitespace trimmed, ASCII case ignored, _ equivalent to -.
The layout of the identifier:
| Bits | Content |
|---|---|
| 31 to 17 | The language subtag, when it is 1 to 3 ASCII letters, 5 bits per letter (a is 1), 0 for none |
| 16 to 0 | A 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_localeliteral, usable inconstitems. - With several, it is the embedded locale the current locale resolves to, which can differ from the tag given to
set_locale(fr-CAcan resolve tofr).
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, thenLANG; the first variable holding a usable value wins. - Skips empty,
CandPOSIXvalues (andC.UTF-8). - Normalizes the value to a tag:
fr_FR.UTF-8@eurobecomesfr-FR. - Returns
Nonewhen no variable is usable. - Allocates a
Stringon each call. - Reads the POSIX environment only: Windows and macOS graphical applications should use the
sys-localecrate 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:
- an exact identifier match;
- the same language: the locale that is the language alone (
fr) if embedded, otherwise the first embedded locale of that language (fr-CA); - 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");
}