#embed #macro #i18n #gettext #fluent


Traits and macros to conveniently embed the output of cargo-i18n into your application binary in order to localize it at runtime

30 releases (9 breaking)

0.10.2 Jan 12, 2021
0.9.4 Nov 24, 2020
0.6.0 May 20, 2020

#10 in Build Utils

Download history 93/week @ 2020-10-05 259/week @ 2020-10-12 456/week @ 2020-10-19 453/week @ 2020-10-26 558/week @ 2020-11-02 327/week @ 2020-11-09 450/week @ 2020-11-16 549/week @ 2020-11-23 231/week @ 2020-11-30 269/week @ 2020-12-07 113/week @ 2020-12-14 128/week @ 2020-12-21 866/week @ 2020-12-28 374/week @ 2021-01-04 744/week @ 2021-01-11 316/week @ 2021-01-18

1,752 downloads per month
Used in 8 crates (6 directly)

MIT license

908 lines

i18n-embed crates.io badge docs.rs badge license badge github actions badge

This library contains traits and macros to conveniently embed the output of cargo-i18n into your application binary in order to localize it at runtime.

Currently this library depends on rust-embed to perform the actual embedding of the language files. This may change in the future to make the library more convenient to use.


Optional Features

The i18n-embed crate has the following optional Cargo features:

  • fluent-system
    • Enable support for the fluent localization system via the FluentLanguageLoader.
  • gettext-system
  • desktop-requester
    • Enables a convenience implementation of LanguageRequester trait called DesktopLanguageRequester for the desktop platform (windows, mac, linux),which makes use of the locale_config crate for resolving the current system locale.
  • web-sys-requester
    • Enables a convenience implementation of LanguageRequester trait called WebLanguageRequester which makes use of the web-sys crate for resolving the language being requested by the user's web browser in a WASM context.


The following is a minimal example for how localize your binary using this library using the fluent localization system.

First you need to compile i18n-embed in your Cargo.toml with the fluent-system and desktop-requester features enabled:

i18n-embed = { version = "0.10", features = ["fluent-system", "desktop-requester"]}
rust-embed = "5"
unic-langid = "0.9"

Set up a minimal i18n.toml in your crate root to use with cargo-i18n (see cargo i18n for more information on the configuration file format):

# (Required) The language identifier of the language used in the
# source code for gettext system, and the primary fallback language
# (for which all strings must be present) when using the fluent
# system.
fallback_language = "en-GB"

# Use the fluent localization system.
# (Required) The path to the assets directory.
# The paths inside the assets directory should be structured like so:
# `assets_dir/{language}/{domain}.ftl`
assets_dir = "i18n"

Next, you want to create your localization resources, per language fluent files. language needs to conform to the Unicode Language Identifier standard, and will be parsed via the unic_langid crate.

The directory structure should look like so:


Then you can instantiate your language loader and language requester:

use i18n_embed::{DesktopLanguageRequester, fluent::{
    FluentLanguageLoader, fluent_language_loader
use rust_embed::RustEmbed;

#[folder = "i18n"] // path to the compiled localization resources
struct Translations;

fn main() {
    let translations = Translations {};
    let language_loader: FluentLanguageLoader = fluent_language_loader!();

    // Use the language requester for the desktop platform (linux, windows, mac).
    // There is also a requester available for the web-sys WASM platform called
    // WebLanguageRequester, or you can implement your own.
    let requested_languages = DesktopLanguageRequester::requested_languages();
    let _result = i18n_embed::select(
        &language_loader, &translations, &requested_languages);

    // continue on with your application

To access localizations, you can use FluentLanguageLoader's methods directly, or, for added compile-time checks/safety, you can use the fl!() macro. Having an i18n.toml configuration file enables you to do the following:

  • Use the cargo i18n tool to perform validity checks (not yet implemented).
  • Integrate with a code-base using the gettext localization system.
  • Use the fluent::fluent_language_loader!() macro to pull the configuration in at compile time to create the fluent::FluentLanguageLoader.
  • Use the fl!() macro to have added compile-time safety when accessing messages.

For more examples, see the documentation for i18n-embed.


~80K SLoC