embedded-i18n 0.2.0

Compile-time i18n for embedded and no_std Rust: translations from JSON, YAML, TOML, gettext PO/MO or Fluent files compiled into ROM literals, with plurals, context, fallbacks and no allocator.
Documentation

embedded-i18n

crates.io docs.rs CI codecov License: MIT MSRV 1.85 no_std

Compile-time internationalization (i18n) and localization (l10n) for embedded Rust, no_std firmware and microcontrollers.

embedded-i18n translates your strings at build time: translations from JSON, YAML, TOML, GNU gettext PO/MO or Fluent files are compiled into your binary as plain &'static str literals stored in flash/ROM. No runtime parser, no allocator, no file system, no heap, and a missing translation is a compile error, not a blank screen on a device in the field.

The API follows rust-i18n: i18n! configures the crate and t! translates, with the same names and most of the same syntax.

Why embedded-i18n?

  • Zero runtime cost. Each t!("key") expands to a string literal. Your firmware only contains the strings it uses, in the language you build for.
  • Fits on a microcontroller. #![no_std], no allocator, no std::fs, no libc. Builds for bare-metal targets such as thumbv7em-none-eabihf and thumbv6m-none-eabi (no atomic compare-and-swap).
  • Mistakes caught by the compiler. Missing keys, missing plural forms, invalid locale files and format placeholders that do not match their arguments are reported as compile errors at the call site. An optional strict mode forbids falling back to another language.
  • Real-world linguistics. CLDR plural rules for 40 languages, gettext Plural-Forms, message context (msgctxt), select variants (gender, enums), locale fallback chains (fr-CA → fr → en), right-to-left bidi isolation and localized dates without libc.
  • Use the files translators already know. JSON (nested keys), YAML, TOML, GNU gettext .po / .mo, and Fluent .ftl, all parsed at compile time, so the formats add no code to the firmware.
  • Runtime language switching when you need it. Embed several locales with available_locales and switch with set_locale("de"), still without allocation.

Example

locales/fr.json, next to Cargo.toml:

{
  "Settings": "Paramètres",
  "files": { "one": "{count} fichier", "other": "{count} fichiers" }
}

src/main.rs (or lib.rs):

use core::fmt::Write;
use embedded_i18n::t;

embedded_i18n::i18n!("locales", default_locale = "fr");

const TITLE: &str = t!("Settings"); // "Paramètres", a literal in flash

fn render(display: &mut impl Write, count: usize) -> core::fmt::Result {
    display.write_str(TITLE)?;
    write!(display, "{}", t!("files", count = count)) // "1 fichier", "3 fichiers", without allocation
}

Build the same code in another language with EMBEDDED_I18N_LOCALE=de cargo build --release. The Getting Started tutorial walks through it, from an empty project to runtime language switching.

Comparison

embedded-i18n rust-i18n Fluent gettext-rs
no_std, no allocator ✅ ❌ ❌ ❌
Translations resolved At compile time, as literals Compile-time embedded, looked up at runtime Parsed and resolved at runtime Loaded from .mo files at runtime
Missing translation Compile error Runtime fallback Runtime error / fallback Runtime fallback to the source string
Format placeholders checked at compile time ✅ (every locale) ❌ ❌ ❌
Plural forms ✅ CLDR rules and gettext Plural-Forms Not built in ✅ ✅
Message context / select variants ✅ / ✅ ❌ / ❌ Message ids / ✅ ✅ / ❌
Runtime locale switching Optional (available_locales) ✅ ✅ ✅
File formats JSON, YAML, TOML, PO, MO, Fluent (subset) YAML, JSON, TOML Fluent MO (via GNU gettext)
Localized dates without libc ✅ ❌ ❌ ❌

Pick embedded-i18n when the binary must be small, deterministic and allocation-free, and the set of languages is known at build time. On desktop or server applications with downloadable language packs and full Fluent features (functions, parameterized terms), a runtime library is a better fit. See the comparison for details.

Documentation

The documentation follows the Diátaxis structure. It is also available on docs.rs, with every example compiled and tested.

Tutorials

  • Getting Started: from an empty project to a program translated into two languages, without allocator.

How-to Guides

Reference

  • The t! macro: grammar, options, result types, placeholders, plural, select, context and compile errors
  • Configuration: i18n!, [package.metadata.i18n], environment overrides, locale resolution and rebuild tracking
  • Locale files: JSON, YAML, TOML, PO, MO and Fluent, and their Cargo features
  • Plural rules: CLDR categories, supported languages and gettext Plural-Forms
  • Runtime API: set_locale, Locale, locale!, available_locales!, system_locale
  • Date and time: tokens, supported languages and API
  • Unicode ranges
  • Features and no_std: Cargo features, allocation and targets

Explanation

Cargo Features

Feature Description
backend-json Default. JSON locale files
backend-yaml, backend-toml YAML and TOML locale files
backend-po, backend-mo GNU gettext .po and compiled .mo locale files
backend-fluent Fluent .ftl locale files (simple messages)
alloc String / Vec returning helpers
std Enables alloc, and system_locale() to read the locale of the operating system

The backends only affect the procedural macros: they add no code and no dependency to the firmware.

Minimum Supported Rust Version

Rust 1.85 (edition 2024). Raising it is not considered a breaking change.

Contributing

Issues and pull requests are welcome. Pull request titles follow Conventional Commits (feat: ..., fix: ...), as they become the changelog; releases are automated, see RELEASING.md.

License

Licensed under the MIT License.


Developed by Alix ANNERAUD.