Skip to content

Commit

Permalink
feat: add lang setting
Browse files Browse the repository at this point in the history
add lang setting command `dcli style lang`
  • Loading branch information
PrivateRookie committed Dec 14, 2020
1 parent a6aac0e commit 5834716
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "dcli"
version = "0.0.3"
authors = ["PrivateRookie <996514515@qq.com>"]
edition = "2018"
description = "MySQL 数据库连接管理工具"
description = "MySQL 数据库连接管理工具 | MySQL connection manage tool"
license-file = "LICENSE"
readme = "README.md"

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ AsciiFull AsciiMd Utf8Full Utf8HBorderOnly

但 dcli 属于早期阶段,所以很多功能仍然不完整,如有问题请开 ISSUE。

### 设置语言

默认情况下 dcli 会尝试读取本地语言设置,自动设置语言. 如果这不和预期, 可以试用

`dcli style lang` 命令设置语言. 目前支持 `zh-CN``en-US` 两种语言.

### 其他命令

dcli 使用 structopt 构建命令工具,当你有疑问时可以运行 `dcli help <子命令>` 查看帮助信息。
Expand Down
15 changes: 13 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::fl;
use crate::{
config::{Config, Profile, SslMode, TableStyle},
config::{Config, Lang, Profile, SslMode, TableStyle},
mysql::connect,
utils::read_file,
};
use anyhow::{anyhow, Context, Result};
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use comfy_table::*;
use crate::fl;
use sqlx::{
mysql::MySqlRow, types::time::Date, types::time::Time, Column, Row, TypeInfo, Value, ValueRef,
};
Expand Down Expand Up @@ -163,6 +163,13 @@ pub enum StyleCmd {
)]
style: TableStyle,
},
#[cfg_attr(feature = "zh-CN", doc = "设置语言")]
#[cfg_attr(feature = "en-US", doc = "set language")]
Lang {
#[cfg_attr(feature = "zh-CN", doc = "语言, 可选 en-US, zh-CN")]
#[cfg_attr(feature = "en-US", doc = "lang, options: en-US, zh-CN")]
name: Option<Lang>,
},
}

impl DCliCommand {
Expand All @@ -174,6 +181,10 @@ impl DCliCommand {
config.table_style = style.clone();
config.save()?;
}
StyleCmd::Lang { name } => {
config.lang = name.clone();
config.save()?;
}
};
Ok(())
}
Expand Down
40 changes: 40 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,46 @@ use crate::fl;
pub struct Config {
pub profiles: HashMap<String, Profile>,
pub table_style: TableStyle,
pub lang: Option<Lang>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Lang {
#[serde(rename = "en-US")]
EnUS,
#[serde(rename = "zh-CN")]
ZhCN,
}

impl Default for Lang {
fn default() -> Self {
Lang::EnUS
}
}

impl FromStr for Lang {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let lower = s.to_ascii_lowercase();
if lower.starts_with("en-us") {
return Ok(Lang::EnUS);
} else if lower.starts_with("zh-cn") {
return Ok(Lang::ZhCN);
} else {
Err(anyhow!(fl!("invalid-value", val = s)))?
}
}
}

impl ToString for Lang {
fn to_string(&self) -> String {
match self {
Lang::EnUS => "en-US",
Lang::ZhCN => "zh-CN",
}
.to_string()
}
}

#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)]
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Mutex;
use std::sync::{Arc, Mutex};

use anyhow::Result;
use cli::DCliCommand;
Expand Down Expand Up @@ -26,14 +26,13 @@ pub mod config;
pub mod mysql;
pub mod utils;

pub static LOADER: Lazy<Mutex<FluentLanguageLoader>> = Lazy::new(|| {
pub static LOADER: Lazy<Arc<Mutex<FluentLanguageLoader>>> = Lazy::new(|| {
let translations = Translations {};

let language_loader: FluentLanguageLoader = fluent_language_loader!();
let requested_languages = DesktopLanguageRequester::requested_languages();
let _result = i18n_embed::select(&language_loader, &translations, &requested_languages);
language_loader.set_use_isolating(false);
Mutex::new(language_loader)
Arc::new(Mutex::new(language_loader))
});

#[macro_export]
Expand All @@ -52,6 +51,9 @@ async fn main() -> Result<()> {
let cmd = DCliCommand::from_args();
// init_log();
let mut config = Config::load()?;
if let Some(lang) = &config.lang {
utils::reset_loader(lang)
}
cmd.run(&mut config).await?;
Ok(())
}
Expand Down
18 changes: 17 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::fl;
use crate::{config::Lang, fl, Translations};
use anyhow::{Context, Result};
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
unic_langid::LanguageIdentifier,
};
use std::io::Read;

pub fn read_file(path: &str) -> Result<String> {
Expand All @@ -10,3 +14,15 @@ pub fn read_file(path: &str) -> Result<String> {
.with_context(|| fl!("read-file-failed", file = path))?;
Ok(content)
}

pub fn reset_loader(lang: &Lang) {
let lang = match lang {
Lang::ZhCN => lang.to_string().parse::<LanguageIdentifier>().unwrap(),
Lang::EnUS => lang.to_string().parse::<LanguageIdentifier>().unwrap(),
};
let translations = Translations {};
let language_loader: FluentLanguageLoader = fluent_language_loader!();
let _result = i18n_embed::select(&language_loader, &translations, &vec![lang]);
language_loader.set_use_isolating(false);
*crate::LOADER.lock().unwrap() = language_loader
}

0 comments on commit 5834716

Please sign in to comment.