diff --git a/Cargo.lock b/Cargo.lock index 5b4989e..a9bba5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -690,6 +690,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "cstr_core" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd98742e4fdca832d40cab219dc2e3048de17d873248f83f17df47c1bea70956" +dependencies = [ + "cty", + "memchr", +] + [[package]] name = "ctr" version = "0.8.0" @@ -699,6 +709,12 @@ dependencies = [ "cipher", ] +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + [[package]] name = "curve25519-dalek" version = "3.2.0" @@ -2613,6 +2629,21 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "sys-locale" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "658ee915b6c7b73ec4c1ffcd838506b5c5a4087eadc1ec8f862f1066cf2c8132" +dependencies = [ + "cc", + "cstr_core", + "js-sys", + "libc", + "wasm-bindgen", + "web-sys", + "winapi", +] + [[package]] name = "take_mut" version = "0.2.2" @@ -2657,6 +2688,7 @@ dependencies = [ "serde_yaml", "singleton", "sled", + "sys-locale", "teloxide", "teloxide-core", "teloxide-macros", diff --git a/Cargo.toml b/Cargo.toml index 4306784..28e6acc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ tracing-subscriber = { version = "0.3.15", default-features = false, features = colored = "2.0.0" time = { version = "0.3.11", features = ["macros", "local-offset"] } chrono = "0.4.19" +sys-locale = "0.2.1" # rust-i18n = "0.6.1" # rust-i18n = { path = "libs/rust-i18n" } rust-i18n = { branch = "main", git = "https://github.com/Itsusinn/rust-i18n.git"} @@ -35,6 +36,7 @@ serde = { version = "1.0.140", default-features = false, features = ["derive","r serde_json = "1.0.82" serde_yaml = "0.8.26" + # asynchronous tokio = { version = "1.20.0", default-features = false, features = ["macros","signal","rt-multi-thread"] } tokio-util = "0.7.3" diff --git a/locales/en-US.yml b/locales/en-US.yml index 4ff6fc8..2c5d431 100644 --- a/locales/en-US.yml +++ b/locales/en-US.yml @@ -6,3 +6,4 @@ en-US: log.boot-sucess: "MesagistoTG activation successful." log.log-callback-error: "An error occurred in the Teloxide dispatcher." log.shutdown: "MesagistoTG is about to close." + log.locale-not-configured: "Localization option is not set, system language preference %{locale_} will be used" \ No newline at end of file diff --git a/locales/zh-CN.yml b/locales/zh-CN.yml index d6a27d5..3003141 100644 --- a/locales/zh-CN.yml +++ b/locales/zh-CN.yml @@ -5,4 +5,5 @@ zh-CN: log.boot-start: "信使TG正在启动, version: v%{version}" log.boot-sucess: "信使TG启动成功" log.log-callback-error: "Teloxide调度器发生错误" - log.shutdown: "TG信使即将关闭" \ No newline at end of file + log.shutdown: "TG信使即将关闭" + log.locale-not-configured: "本地化选项未设置, 将使用系统语言偏好 %{locale_}" diff --git a/src/command.rs b/src/command.rs index e01e43b..2462f43 100644 --- a/src/command.rs +++ b/src/command.rs @@ -8,7 +8,10 @@ use teloxide::{ use crate::{bot::BotRequester, config::CONFIG, message::handlers}; #[derive(BotCommands, Clone)] -#[command(rename = "lowercase", description = "MesagistoTG supports following commands")] +#[command( + rename = "lowercase", + description = "MesagistoTG supports following commands" +)] pub enum Command { #[command(description = "About")] About, diff --git a/src/config.rs b/src/config.rs index 1121742..65d5798 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,7 @@ use dashmap::DashMap; pub struct Config { #[educe(Default = false)] pub enable: bool, - #[educe(Default = "en-US")] + #[educe(Default = "")] pub locale: ArcStr, // A-z order pub bindings: DashMap, diff --git a/src/dispatch.rs b/src/dispatch.rs index ab5f4c4..050ccc9 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -31,9 +31,9 @@ pub async fn start(bot: &BotRequester) { // info!(target: TARGET,"TG信使启动成功"); info!(target: TARGET, "{}", t!("log.boot-sucess")); Dispatcher::builder(bot.clone(), handler) - .error_handler(LoggingErrorHandler::with_custom_text( - t!("log.log-callback-error"), - )) + .error_handler(LoggingErrorHandler::with_custom_text(t!( + "log.log-callback-error" + ))) .build() .dispatch() .await; diff --git a/src/main.rs b/src/main.rs index feca83a..5ba5d3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,14 @@ async fn main() -> Result<()> { async fn run() -> Result<()> { Config::reload().await?; - rust_i18n::set_locale(&CONFIG.locale); + if &CONFIG.locale != "" { + rust_i18n::set_locale(&CONFIG.locale); + } else { + use sys_locale::get_locale; + let locale = get_locale().unwrap_or_else(|| String::from("en-US")).replace("_", "-"); + rust_i18n::set_locale(&locale); + info!(target:TARGET, "{}", t!("log.locale-not-configured",locale_ = &locale)); + } if !CONFIG.enable { warn!(target: TARGET, "{}", t!("log.not-enable")); warn!(target: TARGET, "{}", t!("log.not-enable-helper")); @@ -95,6 +102,6 @@ async fn run() -> Result<()> { }); tokio::signal::ctrl_c().await?; CONFIG.save().await.expect("保存配置文件失败"); - info!(target: TARGET,"{}",t!("log.shutdown")); + info!(target: TARGET, "{}", t!("log.shutdown")); Ok(()) }