优化i18n
This commit is contained in:
parent
baccd91c50
commit
d720fb8c70
@ -60,16 +60,20 @@ pub fn lang(lang_id: &str, message_id: MessageId) -> &'static str {
|
||||
|
||||
/// 将模板消息转换为最终消息,支持模板占位符
|
||||
pub fn convert_message_with_params(message_template: &'static str, args: Vec<&str>) -> String {
|
||||
if message_template.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
|
||||
if message_template.contains("{}") {
|
||||
let message_split_array: Vec<&str> = message_template.split("{}").collect();
|
||||
// tracing::info!("message_template: {:?}, args: {:?}", message_split_array, args);
|
||||
let mut message = String::new();
|
||||
for (index, message_ele) in message_split_array.iter().enumerate() {
|
||||
message += message_ele;
|
||||
if index < args.len() && index != message_split_array.len() - 1 {
|
||||
message += args[index];
|
||||
}
|
||||
}
|
||||
let capacity = message_template.len() + args.iter().map(|s| s.len()).sum::<usize>();
|
||||
let mut message = String::with_capacity(capacity);
|
||||
|
||||
message_split_array.iter().zip(args.iter().chain(std::iter::once(&""))).for_each(|(part, arg)| {
|
||||
message.push_str(part);
|
||||
message.push_str(arg);
|
||||
});
|
||||
|
||||
message
|
||||
} else {
|
||||
String::from(message_template)
|
||||
@ -86,10 +90,7 @@ macro_rules! message {
|
||||
($lang_id:expr, $message_id:expr, $($arg:expr),*) => {
|
||||
{
|
||||
let message_template: &'static str = i18n::lang($lang_id, $message_id);
|
||||
let mut args: Vec<&str> = vec![];
|
||||
$(
|
||||
args.push($arg);
|
||||
)*
|
||||
let args = vec![$($arg),*];
|
||||
i18n::convert_message_with_params(message_template, args)
|
||||
}
|
||||
};
|
||||
|
@ -8,6 +8,7 @@ use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||
use crate::{cache::inner_cache::LOGIN_ACCOUNT_CACHE, config, context::Context, model::response::ResErr, token::Claims, utils::request_util};
|
||||
|
||||
const WHITE_LIST: &[(&str, &str)] = &[
|
||||
("GET", "/"),
|
||||
("POST", "/account/sys"),
|
||||
("POST", "/account/google"),
|
||||
("GET", "/wechat/access_token"),
|
||||
|
@ -2,6 +2,9 @@ use axum::{body::Body, extract::Request, http, routing::get, Router};
|
||||
use library::{config, task};
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::trace::TraceLayer;
|
||||
use i18n::message;
|
||||
use i18n::message_ids::MessageId;
|
||||
use library::context::Context;
|
||||
|
||||
mod controller;
|
||||
mod service;
|
||||
@ -44,7 +47,7 @@ fn init() -> Router {
|
||||
// layer之间存在顺序依赖,勿改。layer执行顺序和配置顺序一致
|
||||
// fallback路由放到最后,如果无匹配的路由,则不会执行layer,直接返回404
|
||||
Router::new()
|
||||
.route("/", get(|| async { "hello" }))
|
||||
.route("/", get(hello))
|
||||
.nest(&config!().server.prefix_url, auth)
|
||||
.layer(
|
||||
ServiceBuilder::new()
|
||||
@ -64,3 +67,7 @@ fn init() -> Router {
|
||||
)
|
||||
.fallback(|| async { (http::status::StatusCode::NOT_FOUND, "Not Found") })
|
||||
}
|
||||
|
||||
pub async fn hello(context: Context) -> String {
|
||||
message!(context.get_lang_tag(), MessageId::Hello, "川岳").to_string()
|
||||
}
|
Loading…
Reference in New Issue
Block a user