Skip to content

Commit

Permalink
feat: using system locale when not confiugred
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Jul 25, 2022
1 parent 1693e0f commit d52d0e6
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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信使即将关闭"
log.shutdown: "TG信使即将关闭"
log.locale-not-configured: "本地化选项未设置, 将使用系统语言偏好 %{locale_}"
5 changes: 4 additions & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64, ArcStr>,
Expand Down
6 changes: 3 additions & 3 deletions src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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(())
}

0 comments on commit d52d0e6

Please sign in to comment.