2 unstable releases

0.5.0 Nov 30, 2021
0.4.0 Nov 26, 2021

#335 in Internationalization (i18n)

MIT license

23KB
551 lines

Loon

CI Docs Crates.io

Loon is a localization/internationalization library, inspired by ruby-i18n.

It use rust-embed for embed the localization assets into your binary.

Usage

Load locales assets by RustEmbed and init Loon in your lib.rs

use rust_embed::RustEmbed;

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

// Use RustEmbed to locale assets
#[derive(RustEmbed)]
#[folder = "locales/"]
#[include = "*.yml"]
struct Asset;

fn main() {
    loon_embed::init::<Asset>("en");
}

You must put I18n YAML files in locales/ folder.

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

For example of en.yml:

greeting: Hello world
messages:
  hello: Hello, {}

Now you can use t! macro in anywhere.

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

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

You can use loon_embed::set_locale or call loon_embed::init agian to change the current locale in runtime.

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

License

MIT

Dependencies

~2.2–3.5MB
~53K SLoC