Expand description
Compile-time internationalization for embedded and no_std Rust, with a
rust-i18n style API.
Translations are read from locale files at build time by procedural macros
and embedded in the binary as &'static str literals: no runtime parsing, no
allocator, no file system, and a missing translation is a compile error at
the call site.
§Quick start
Put locale files in a locales/ directory next to Cargo.toml:
{
"greeting": "Hello",
"files": { "one": "{count} file", "other": "{count} files" }
}Call i18n! once at the crate root, before any mod declaration, then
translate with t!:
embedded_i18n::i18n!("locales");
use core::fmt::Write;
use embedded_i18n::t;
const GREETING: &str = t!("greeting");
assert_eq!(GREETING, "Hello");
let mut output = String::new();
write!(output, "{}", t!("with_arg", value = 42))?;
assert_eq!(output, "Value: 42");
assert_eq!(t!("files", count = 2).to_string(), "2 files");§Macros
| Macro | Result |
|---|---|
i18n! | Configures the translations of the crate, once at its root |
t! | &'static str (or &'static CStr) without arguments, a core::fmt::Display value with arguments or count |
locale! | Name of the locale the crate translates into |
available_locales! | Locales embedded in the crate |
§Configuration
i18n! takes the locale directory and options, read from
[package.metadata.i18n] in Cargo.toml when not given:
i18n! argument | [package.metadata.i18n] key | Default | Meaning |
|---|---|---|---|
| first positional string | load-path | "locales" | Locale directory, relative to Cargo.toml |
fallback = "en" or ["en", "es"] | fallback | ["en"] | Fallback chain after the locale and its parents |
default_locale = "fr" | default-locale | "en" | Locale active at startup |
available_locales = [..] | available-locales | [default_locale] | Embedded locales; more than one enables runtime selection |
strict = true | strict | false | Forbids falling back for messages missing from the primary locale |
bidi_isolation = true | bidi-isolation | false | Isolates placeholders of right-to-left locales |
The EMBEDDED_I18N_LOCALE and EMBEDDED_I18N_LOCALES (comma separated)
environment variables override default_locale and available_locales in
every crate of the build, to build one firmware per language. Cargo rebuilds the crate when a locale
file, Cargo.toml or one of these variables changes.
Locales are normalized like BCP 47 tags and resolved from the most specific
to the most generic (fr-CA, fr, then the fallbacks). The special locale
pseudo generates a pseudo-localization of the fallback locales.
§Runtime locales
With several available_locales, set_locale selects the locale at
runtime, for every crate of the program: each one translates into its
closest embedded locale, or its default_locale. t!("key", locale = "fr") translates into a given locale without touching the global one.
embedded_i18n::i18n!("locales", available_locales = ["en", "fr"]);
use embedded_i18n::{locale, set_locale, t};
fn main() {
assert_eq!(t!("greeting", locale = "fr-CA"), "Bonjour");
set_locale("fr");
assert_eq!(t!("greeting"), "Bonjour");
assert_eq!(locale!(), "fr");
set_locale("de");
assert_eq!(t!("greeting"), "Hello");
}§Locale file formats
Each format is enabled by a Cargo feature; all parsing happens in the procedural macro, so formats add nothing to the binary.
| Feature | Files |
|---|---|
backend-json (default) | .json, nested objects flattened to dotted keys |
backend-yaml | .yaml, .yml |
backend-toml | .toml |
backend-po | GNU gettext .po, with msgctxt, plural forms and Plural-Forms |
backend-mo | Compiled GNU gettext .mo |
backend-fluent | Fluent .ftl simple messages and whole-message selects |
§Runtime helpers
DateTimeFormatterandwrite_unix_timestamp: localizedstrftime-style formatting without allocation.is_right_to_left: text direction.get_locale_rangesandwrite_ranges: Unicode ranges to subset fonts.Localeandset_locale: runtime locale selection when several locales are embedded.
§Features
alloc:StringandVecreturning variants of the helpers.std: enablesallocandsystem_locale(), the locale of the operating system.
§Guide
The guide follows the Diátaxis structure:
- Tutorial: getting started, from an empty project to a program translated into two languages.
- How-to guides: switch language at runtime, localize per request, build one firmware per language, use gettext PO files and other formats, ship complete translations, pseudo-localize, support right-to-left languages, format dates and times, subset fonts, migrate from rust-i18n and migrate from 0.1.
- Reference: the
t!macro, configuration, locale files, plural rules, runtime API, date and time, Unicode ranges and features andno_std. - Explanation: how it works, the runtime locale model, design choices and a comparison with other crates.
Modules§
- guide
- User guide for
embedded-i18n, organized in four parts:
Macros§
- available_
locales - Lists the locales embedded in the calling crate, as
&'static [&'static str], in configuration order. - i18n
- Configures the translations of the calling crate.
- locale
- Returns the name of the locale the calling crate translates into, as a
&'static str. - t
- Translates a message key, at compile time.
Structs§
- Date
Time Display - Timestamp formatted on display, returned by
DateTimeFormatter::display. - Date
Time Formatter - Formats Unix timestamps with
strftime-style patterns, localized and without allocation. - Date
Time Locale - Month and weekday names, AM/PM markers and default patterns of a locale,
used by
DateTimeFormatter. - Locale
- A locale, packed into a 32-bit identifier.
Constants§
- ARABIC
- Arabic code points, as a half-open range.
- ARABIC_
SUPPLEMENT - Arabic Supplement code points, as a half-open range.
- BASIC_
LATIN - Basic Latin (printable ASCII) code points, as a half-open range.
- CJK_
UNIFIED_ IDEOGRAPHS - CJK Unified Ideographs code points, as a half-open range.
- CYRILLIC
- Cyrillic code points, as a half-open range.
- CYRILLIC_
SUPPLEMENT - Cyrillic Supplement code points, as a half-open range.
- DEVANAGARI
- Devanagari code points, as a half-open range.
- ENGLISH
- English (United States) names and patterns.
- GREEK
- Greek and Coptic code points, as a half-open range.
- GREEK_
EXTENDED - Greek Extended code points, as a half-open range.
- HANGUL_
SYLLABLES - Hangul Syllables code points, as a half-open range.
- HEBREW
- Hebrew code points, as a half-open range.
- HIRAGANA
- Hiragana code points, as a half-open range.
- KATAKANA
- Katakana code points, as a half-open range.
- LATIN_
1_ SUPPLEMENT - Latin-1 Supplement code points, as a half-open range.
- LATIN_
EXTENDED_ A - Latin Extended-A code points, as a half-open range.
- LATIN_
EXTENDED_ ADDITIONAL - Latin Extended Additional code points, as a half-open range.
- LATIN_
EXTENDED_ B - Latin Extended-B code points, as a half-open range.
- THAI
- Thai code points, as a half-open range.
- VIETNAMESE_
EXTENSIONS - Vietnamese letters of Latin Extended Additional code points, as a half-open range.
Functions§
- date_
time_ locale - Returns the built-in names and patterns of a locale, English if the language is not covered.
- find_
date_ time_ locale - Returns the built-in names and patterns of a locale, or
Noneif the language is not covered. - format_
range - Formats a range like
write_range. Available with theallocfeature. - format_
ranges - Formats ranges like
write_ranges. Available with theallocfeature. - format_
unix_ timestamp - Formats a Unix timestamp into a
String, in English and UTC. For another locale, useDateTimeFormatter. Available with theallocfeature. - get_
locale_ ranges - Returns the Unicode ranges needed to render a language, to subset fonts.
- is_
right_ to_ left - Returns whether a locale is written right to left.
- merge_
contiguous_ ranges - Sorts ranges and merges overlapping or adjacent ones. Available with the
allocfeature. - merge_
contiguous_ ranges_ in_ place - Sorts ranges and merges overlapping or adjacent ones, in place and without allocation.
- set_
locale - Selects the current locale used by
t!andlocale!. - system_
locale - Returns the locale of the operating system, from the
LC_ALL,LC_MESSAGESandLANGenvironment variables, as a BCP 47 style tag (fr_FR.UTF-8becomesfr-FR). Available with thestdfeature. - write_
range - Writes a range as
start-end(inclusive end), orstartfor a single code point. - write_
ranges - Writes ranges as a comma separated list, as accepted by font subsetting tools.
- write_
unix_ timestamp - Writes a Unix timestamp formatted with a pattern, in English and UTC. For
another locale, use
DateTimeFormatter. SeeDateTimeFormatterfor the tokens.