From 475e74d7bf9b518171bc30ca9f2bbce3ad3c53f6 Mon Sep 17 00:00:00 2001 From: Fankai Liu Date: Thu, 9 Nov 2023 21:54:35 +0800 Subject: [PATCH] can set the language when using the command --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/i18n.rs | 26 ++++++++++++++++++++------ src/main.rs | 4 +++- 5 files changed, 77 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f2bccaf..4cc1cda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,7 +798,7 @@ checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] name = "salvo-cli" -version = "0.1.18" +version = "0.1.19" dependencies = [ "ansi_term", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 7d9d57f..e3abfe2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "salvo-cli" -version = "0.1.18" +version = "0.1.19" edition = "2021" authors = ["Fankai Liu liufankai137@outlook.com"] keywords = ["salvo", "cli","template"] diff --git a/README.md b/README.md index b56cc72..0d34a23 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,59 @@ cargo install salvo-cli To create a new Salvo project, use the new command followed by the name of your project: ```bash +//use the local language salvo-cli new project_name + +// Use English +salvo-cli new project_name --lang=en + +// 使用简体中文 +salvo-cli new project_name --lang=zh + +// 使用繁體中文 +salvo-cli new project_name --lang=zh_TW + +// Utilisez le français +salvo-cli new project_name --lang=fr + +// 日本語を使用する +salvo-cli new project_name --lang=ja + +// Usa el español +salvo-cli new project_name --lang=es + +// Verwenden Sie Deutsch +salvo-cli new project_name --lang=de + +// Используйте русский +salvo-cli new project_name --lang=ru + +// Usa l `italiano +salvo-cli new project_name --lang=it + +// Use o português +salvo-cli new project_name --lang=pt + +// 한국어를 사용하십시오 +salvo-cli new project_name --lang=ko + +// Bruk norsk +salvo-cli new project_name --lang=no + +// Notaðu íslensku +salvo-cli new project_name --lang=is + +// Використовуйте українську +salvo-cli new project_name --lang=uk + +// ใช้ภาษาไทย +salvo-cli new project_name --lang=th + +// Χρησιμοποιήστε την ελληνική +salvo-cli new project_name --lang=el + +// Brug dansk +salvo-cli new project_name --lang=da ``` ## Update diff --git a/src/i18n.rs b/src/i18n.rs index 7d3e98d..36a3a5f 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -1,11 +1,25 @@ -pub fn set_locale() { - match get_language() { +const SUPPORTED_LANGUAGES: [&str; 17] = [ + "en", "zh_CN", "zh_TW", "fr", "ja", "es", "de", "ru", "it", "pt", "ko", "no", "is", "uk", "th", + "el", "da", +]; +pub fn set_locale(language: &Option) { + match language { Some(lang) => { - rust_i18n::set_locale(lang.as_str()); - } - None => { - rust_i18n::set_locale("en"); + let lang = if lang == "zh" { "zh_CN" } else { lang }; + if SUPPORTED_LANGUAGES.contains(&lang) { + rust_i18n::set_locale(lang); + } else { + rust_i18n::set_locale("en"); + } } + None => match get_language() { + Some(lang) => { + rust_i18n::set_locale(lang.as_str()); + } + None => { + rust_i18n::set_locale("en"); + } + }, } } fn get_language() -> Option { diff --git a/src/main.rs b/src/main.rs index 6c3714d..e246ea8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,13 +19,15 @@ enum SubCommand { #[derive(Parser, Debug, Clone)] pub struct Project { pub project_name: String, + #[clap(short, long)] + lang: Option, } fn main() -> Result<()> { - set_locale(); utils::print_logo(); let opts: Opts = Opts::parse(); match opts.subcmd { SubCommand::New(project) => { + set_locale(&project.lang); match utils::create_project(project) { Ok(_) => (), Err(e) => utils::error(e.to_string()),