Crate rust_i18n

source ·
Expand description

CI Docs Crates.io

Rust I18n is use Rust codegen for load YAML file storage translations on compile time, and give you a t! macro for simply get translation texts.

Inspired by ruby-i18n.

Usage

Add crate dependencies in your Cargo.toml:

[dependencies]
rust-i18n = "0"

Load macro and init translations in lib.rs

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

// Init translations for current crate.
i18n!("locales");

You must put I18n YAML files in locales/ folder.

locales/
├── en.yml
├── zh-CN.yml

For example of en.yml:

hello: Hello world
messages:
  hello: Hello, %{name}

Now you can use t! macro in anywhere.

t!("hello");
// => "Hello world"

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

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

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

You can use rust_i18n::set_locale to change the current locale in runtime.

rust_i18n::set_locale("zh-CN");
rust_i18n::locale();
// => "zh-CN"

Macros

  • Init I18n translations.
  • Get I18n text

Functions