Skip to content

Commit

Permalink
feat: i18n support step-1 try rust-i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Jul 24, 2022
1 parent 8899ba2 commit 0e93651
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 5 deletions.
169 changes: 168 additions & 1 deletion Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ version = "1.6.0"
authors = ["Itsusinn <itsusinn@foxmail.com>"]
edition = "2021"

[package.metadata.i18n]
available-locales = ["en-US", "zh-CN"]
default-locale = "en-US"
load-path = "locales"

[features]
default = ["color"]
no-color = ["colored/no-color"]
Expand All @@ -23,6 +28,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"
rust-i18n = "0.6.1"

# serialization
serde = { version = "1.0.140", default-features = false, features = ["derive","rc"] }
Expand Down Expand Up @@ -82,3 +88,4 @@ lto = true
incremental = false
codegen-units = 1
strip = "debuginfo"

3 changes: 3 additions & 0 deletions locales/TODO.en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
en-US: {}

3 changes: 3 additions & 0 deletions locales/TODO.zh-CN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
zh-CN: {}

4 changes: 4 additions & 0 deletions locales/en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
en-US:
not-enable: "Mesagisto-Bot is not enabled and is about to exit the program."
not-enable-helper: "To enable it, please modify the configuration file."
4 changes: 4 additions & 0 deletions locales/zh-CN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
zh-CN:
not-enable: "Mesagisto-Bot未被启用, 即将退出程序。"
not-enable-helper: "若要启用,请修改配置文件。"
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use dashmap::DashMap;
pub struct Config {
#[educe(Default = false)]
pub enable: bool,
#[educe(Default = "en-US")]
pub locale: ArcStr,
// A-z order
pub bindings: DashMap<i64, ArcStr>,
pub cipher: CipherConfig,
Expand Down
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use futures::FutureExt;
use mesagisto_client::MesagistoConfig;
use teloxide::{prelude::*, types::ParseMode, Bot};
use tracing::*;
use rust_i18n::t;

use self::message::handlers;
use crate::config::{Config, CONFIG};
Expand All @@ -17,6 +18,10 @@ extern crate educe;
extern crate automatic_config;
#[macro_use]
extern crate singleton;
#[macro_use]
extern crate rust_i18n;
i18n!("locales");

mod bot;
mod command;
mod config;
Expand All @@ -43,11 +48,10 @@ async fn main() -> Result<()> {

async fn run() -> Result<()> {
Config::reload().await?;
rust_i18n::set_locale(&CONFIG.locale);
if !CONFIG.enable {
warn!("Mesagisto-Bot is not enabled and is about to exit the program.");
warn!("To enable it, please modify the configuration file.");
warn!("Mesagisto-Bot未被启用, 即将退出程序。");
warn!("若要启用,请修改配置文件。");
warn!("{}",t!("not-enable"));
warn!("{}",t!("not-enable-helper"));
return Ok(());
}
CONFIG.migrate();
Expand Down

0 comments on commit 0e93651

Please sign in to comment.