Skip to content

Commit

Permalink
can set the language when using the command
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Nov 9, 2023
1 parent 4b71777 commit 475e74d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "salvo-cli"
version = "0.1.18"
version = "0.1.19"
edition = "2021"
authors = ["Fankai Liu [email protected]"]
keywords = ["salvo", "cli","template"]
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions src/i18n.rs
Original file line number Diff line number Diff line change
@@ -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<String>) {
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<String> {
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ enum SubCommand {
#[derive(Parser, Debug, Clone)]
pub struct Project {
pub project_name: String,
#[clap(short, long)]
lang: Option<String>,
}
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()),
Expand Down

0 comments on commit 475e74d

Please sign in to comment.