Crates.io | loon-embed |
lib.rs | loon-embed |
version | 0.5.0 |
source | src |
created_at | 2021-11-26 12:12:59.861654 |
updated_at | 2021-11-30 11:14:15.387858 |
description | I18n for Rust, support embed YAML, JSON. |
homepage | |
repository | https://github.com/longbridgeapp/loon |
max_upload_size | |
id | 488002 |
size | 22,994 |
Loon is a localization/internationalization library, inspired by ruby-i18n.
It use rust-embed for embed the localization assets into your binary.
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"
MIT