i18n-embed 0.7.0

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

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.

Changelog

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.

Example

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:

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

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

my_crate/
  Cargo.toml
  i18n.toml
  src/
  i18n/
    lang_code/
      my_crate.ftl

Then you can instantiate your language loader and language requester:

use i18n_embed::{I18nEmbed, DesktopLanguageRequester, fluent::{
  FluentLanguageLoader
}};
use rust_embed::RustEmbed;
use unic_langid::LanguageIdentifier;

#[derive(RustEmbed, I18nEmbed)]
#[folder = "i18n"] // path to the localization resources
struct Translations;

fn main() {
    let translations = Translations {};

    let fallback_language: LanguageIdentifier = "en".parse().unwrap();
     let language_loader = FluentLanguageLoader::new(
       "my_crate", fallback_language);

    // 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();

    i18n_embed::select(&language_loader, &translations, &requested_languages);
}

You can also make use of the i18n.toml configuration file, and the cargo i18n tool to integrate with a code-base using gettext, and in the future to perform compile time checks, and use the fluent_language_loader!() macro to pull the configuration at compile time to create the FluentLanguageLoader.

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