5 个版本
0.2.1 | 2024 年 4 月 27 日 |
---|---|
0.2.0 | 2024 年 4 月 26 日 |
0.1.2 | 2022 年 11 月 3 日 |
0.1.1 | 2022 年 11 月 3 日 |
0.1.0 | 2022 年 11 月 3 日 |
在 国际化(i18n) 中排名第 37
每月下载 290 次
160KB
2.5K SLoC
🌍 Tarjama:让你的 Rust 应用程序国际化 🚀
欢迎使用 Tarjama,您在全球范围内让 Rust 应用程序可访问的首选库! 🎉
什么是 Tarjama? 🤔
Tarjama 允许您轻松地将 Rust 应用程序国际化,支持我们 Locale
枚举(src/locale.rs
)中找到的广泛地区。使用 Tarjama,您可以轻松地在语言之间切换,满足全球受众的需求。 🌐
入门 🚀
使用 Cargo 将 Tarjama 添加到您的项目中
cargo add tarjama
或将其添加到您的 Cargo.toml
[dependencies]
tarjama = "0.2.1"
示例用法 🛠
基本用法:
use tarjama::Translator;
use tarjama::context;
// load translations from a directory of toml files
let catalogue_bag = toml::load("path/to/translations").await?;
let mut translator = Translator::from_catalogue_bag(catalogue_bag);
// set the fallback locale to English
translator.set_fallback_locale("en");
// translate the message 'greeting' in the 'messages' domain with the context 'name' set to 'World' in English.
let message_en = translator.trans("en", "messages", "greeting", context!(name = "World"))?;
// translate the message 'greeting' in the 'messages' domain with the context 'name' set to '世界' in Chinese,
// if the message is not found in Chinese, it will fallback to English, as it is set as the fallback locale.
let message_zh = translator.trans("zh", "messages", "greeting", context!(name = "世界"))?;
println!(message);
[!NOTE] 期望翻译目录中的每个消息文件使用
{domain}.{locale}.toml
格式命名。例如,英文地区为messages.en.toml
。
手动目录创建:
Tarjama 允许您手动创建目录包,如下所示
use std::collections::HashMap;
use tarjama::Translator;
use tarjama::context;
use tarjama::catalogue::{Catalogue, CatalogueBag};
use tarjama::locale::{Locale, EnglishVariant};
let catalogue_bag = CatalogueBag::from_catalogues(vec![
Catalogue::from_messages(Locale::English(EnglishVariant::Default), HashMap::from([
("messages".to_owned(), HashMap::from([
("greeting".to_owned(), "Hello, {name}!".to_owned()),
]))
])),
]);
let translator = Translator::from_catalogue_bag(catalogue_bag);
let message = translator.trans("en", "messages", "greeting", context!(name = "World"))?;
println!(message);
[!NOTE] 如果您计划手动创建目录包,则可以从您的
Cargo.toml
中移除toml
功能。cargo add tarjama --no-default-features
这将减小二进制文件的大小,并移除对
toml
包的依赖。
Actix Web 集成 🌐
Tarjama 提供了 Actix Web 中间件,以便轻松与 Actix Web 应用程序集成。要使用中间件,请将 actix-web
功能添加到您的 Cargo.toml
tarjama = { version = "0.2", features = ["actix-web"] }
然后,将中间件添加到您的 Actix Web 应用程序
use actix_web::{web, App, HttpServer};
use tarjama::actix::TarjamaMiddleware;
use tarjama::Translator;
use tarjama::context;
use tarjama::locale::{Locale, EnglishVariant};
async fn example(translator: Translator, locale: Locale) -> Result<HttpResponse> {
let content = translator.trans(locale, "messages", "greeting", context!(name = "World"))
.map_err(error::ErrorInternalServerError)?;
Ok(HttpResponse::Ok()
.content_type("text/plain")
.body(content))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let translator = Translator::with_catalogue_bag(
load("/path/to/translations/").await.expect("couldn't load translations"),
);
HttpServer::new(move || {
App::new()
.wrap(TranslatorMiddleware::new(translator.clone(), Locale::English(EnglishVariant::Default)))
.route("/", web::get().to(example))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
功能标志 🚩
- actix-web:启用此功能以支持 Actix Web 中间件,允许您在 Actix Web 应用程序中使用 Tarjama 中间件。
- file:对于基本的文件加载操作,此功能对于实现自定义加载器很有用。
- toml:启用此功能以支持 toml 文件,允许您从 toml 文件中加载翻译。
- default:包括
toml
。
许可 📜
Tarjama 在以下两种许可下使用
- Apache许可证,版本2.0(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证(LICENSE-MIT 或 http://opensource.org/licenses/MIT)
请选择最适合您项目需求的许可证。
贡献 🤝
除非您明确声明,否则根据Apache-2.0许可证定义的您有意提交的工作中的任何贡献,都将按上述方式双许可,无需任何附加条款或条件。
深入 🏊
准备好让您的Rust应用程序全球可用吗?今天集成Tarjama,让我们一起打破语言障碍!
依赖项
~0.7–11MB
~126K SLoC