7 releases (2 stable)

1.1.0 Jan 4, 2023
1.0.0 Nov 18, 2022
0.4.0 May 6, 2022
0.3.5 Apr 2, 2022
0.3.4 Dec 20, 2021

#32 in Internationalization (i18n)

Download history 204/week @ 2022-12-02 272/week @ 2022-12-09 349/week @ 2022-12-16 125/week @ 2022-12-23 243/week @ 2022-12-30 623/week @ 2023-01-06 335/week @ 2023-01-13 640/week @ 2023-01-20 682/week @ 2023-01-27 808/week @ 2023-02-03 727/week @ 2023-02-10 466/week @ 2023-02-17 534/week @ 2023-02-24 528/week @ 2023-03-03 651/week @ 2023-03-10 462/week @ 2023-03-17

2,263 downloads per month
Used in 4 crates (via rust-i18n)

MIT license

410 lines

Rust I18n

CI Docs Crates.io

Rust I18n is a crate for loading localized text from a set of YAML mapping files. The mappings are converted into data readable by Rust programs at compile time, and then localized text can be loaded by simply calling the provided t! macro.

The API of this crate is inspired by ruby-i18n and Rails I18n.


  • Codegen on compile time for includes translations into binary.
  • Global t! macro for loading localized text in everywhere.
  • Use YAML for mapping localized text, and support mutiple YAML files merging.
  • cargo i18n Command line tool for checking and extract untranslated texts into YAML files.


Rust I18n also provided a cargo i18n command line tool help you process translations.

$ cargo install rust-i18n


Add crate dependencies in your Cargo.toml and setup I18n config:

once_cell = "1.10.0"
rust-i18n = "1"

# The available locales for your application, default: ["en"].
# available-locales = ["en", "zh-CN"]

# The default locale, default: "en".
# default-locale = "en"

# Path for your translations YAML file, default: "locales".
# This config for let `cargo i18n` command line tool know where to find your translations.
# You must keep this path is same as the path you pass to `rust_i18n::i18n!` method.
# load-path = "locales"

Load macro and init translations in lib.rs

// Load I18n macro, for allow you use `t!` macro in anywhere.
extern crate rust_i18n;

// Init translations for current crate.
// You must keep this path is same as the path you set `load-path` in [package.metadata.i18n] in Cargo.toml.

Or you can import by use directly:

// You must import in each files when you wants use `t!` macro.
use rust_i18n::t;


fn main() {
    println!("{}", t!("hello"));

    // Use `available_locales` method to get all available locales.
    println!("{:?}", available_locales());

Make sure all YAML files (containing the localized mappings) are located in the locales/ folder of the project root directory:

├── Cargo.lock
├── Cargo.toml
├── locales
│   ├── en.yml
│   ├── zh-CN.yml
│   └── zh-HK.yml
└── src
│   └── main.rs
└── sub_app
│   └── locales
│   │   └── en.yml
│   │   └── zh-CN.yml
│   │   └── zh-HK.yml
│   └── src
│   │   └── main.rs
│   └── Cargo.toml

In the YAML files, specify the localization keys and their corresponding values, for example, in en.yml:

hello: Hello world # A simple key -> value mapping
  hello: Hello, %{name} # A nested key.sub_key -> value mapping, in this case "messages.hello" maps to "Hello, %{name}"

And example of the zh-CN.yml:

hello: 你好世界
  hello: 你好,%{name}

Loading Localized Strings in Rust

Import the t! macro from this crate into your current scope:

use rust_i18n::t;

Then, simply use it wherever a localized string is needed:

// => "Hello world"

t!("hello", locale = "zh-CN");
// => "你好世界"

t!("messages.hello", name = "world");
// => "Hello, world"

t!("messages.hello", locale = "zh-CN", name = "Jason");
// => "你好,Jason"

Setting and Getting the Global Locale

You can use rust_i18n::set_locale to set the global locale at runtime, so that you don't have to specify the locale on each t! invocation.


let locale = rust_i18n::locale();
assert_eq!(locale, "zh-CN");

Extract the untranslated texts

Rust I18n providered a i18n bin for help you extract the untranslated texts from the source code and then write into YAML file.

$ cargo install rust-i18n
# Now you have `cargo i18n` command

After that the untranslated texts will be extracted and saved into locales/TODO.en.yml file.

You also can special the locale by use --locale option:

$ cd your_project_root_directory
$ cargo i18n

Checking [en] and generating untranslated texts...
Found 1 new texts need to translate.
Writing to TODO.en.yml

Checking [fr] and generating untranslated texts...
Found 11 new texts need to translate.
Writing to TODO.fr.yml

Checking [zh-CN] and generating untranslated texts...
All thing done.

Checking [zh-HK] and generating untranslated texts...
Found 11 new texts need to translate.
Writing to TODO.zh-HK.yml

Run cargo i18n -h to see details.

$ cargo i18n -h
cargo-i18n 0.5.0
Rust I18n command for help you simply to extract all untranslated texts from soruce code.

It will iter all Rust files in and extract all untranslated texts that used `t!` macro.
And then generate a YAML file and merge for existing texts.


    cargo i18n [OPTIONS] [--] [source]

    -h, --help       Prints help information
    -V, --version    Prints version information

    <source>    Path of your Rust crate root [default: ./]

Debugging the Codegen Process

The RUST_I18N_DEBUG environment variable can be used to print out some debugging infos when code is being generated at compile time.

$ RUST_I18N_DEBUG=1 cargo build


A minimal example of using rust-i18n can be found here.

I18n Ally

I18n Ally is a VS Code extension for helping you translate your Rust project.

You can add i18n-ally-custom-framework.yml to your project .vscode directory, and then use I18n Ally can parse t! marco to show translate text in VS Code editor.




~132K SLoC