5 个不稳定版本
使用旧的 Rust 2015
0.4.1 | 2019 年 6 月 15 日 |
---|---|
0.4.0 | 2019 年 1 月 20 日 |
0.3.1 | 2018 年 11 月 30 日 |
0.3.0 | 2018 年 11 月 29 日 |
0.1.0 | 2018 年 6 月 17 日 |
在 国际化(i18n) 中排名第 123
每月下载量 35
16KB
115 行(不包括注释)
Rocket I18N 
一个帮助您将 Rocket 或 Actix Web 应用程序国际化的 Crate。
它仅选择每个请求的正确区域设置,并返回相应的 gettext::Catalog
。
用法
首先将其添加到您的 Cargo.toml
[dependencies]
rocket_i18n = "0.4"
gettext-macros = "0.1" # Provides proc-macros to manage translations
然后,在您的 main.rs
# use rocket;
use gettext_macros::{compile_i18n, include_i18n, init_i18n};
init_i18n!("my_web_app", en, eo, it, pl);
fn main() {
rocket::ignite()
// Make Rocket manage your translations.
.manage(include_i18n!());
// Register routes, etc
}
compile_i18n!();
然后在所有您的请求中,您将能够使用 i18n
宏来翻译任何内容。它接受一个 gettext::Catalog
和一个要翻译的字符串作为参数。
use gettext_macros::i18n;
use rocket_i18n::I18n;
#[get("/")]
fn route(i18n: I18n) -> &str {
i18n!(i18n.catalog, "Hello, world!")
}
有关 i18n!
的详细说明,请参阅其文档。
您还可以在模板中使用 t
宏,只要它们有一个名为 catalog
的字段来存储您的目录。这对于 Askama 模板特别有用。
与 Actix Web 一起使用
首先,禁用默认功能,以免引入所有 Rocket。
[dependencies.rocket_i18n]
version = "0.4"
default-features = false
features = ["actix-web"]
然后将它添加到您的应用程序中。
use gettext_macros::*;
use rocket_i18n::{I18n, Internationalized, Translations};
fn route_handler(i18n: I18n) -> &str {
i18n!(i18n.catalog, "Hello, world!")
}
#[derive(Clone)]
struct MyState {
translations: Translations,
}
impl Internationalized for MyState {
fn get(&self) -> Translations {
self.translations.clone()
}
}
fn main() {
let state = MyState {
translations: rocket_i18n::i18n("your-domain", vec![ "en", "fr", "de", "ja" ]);
};
App::with_state(state)
.resource("", |r| r.with(route_handler))
.finish();
}
依赖关系
~11–16MB
~301K SLoC