Skip to content

Commit

Permalink
fix bug i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Nov 7, 2023
1 parent 3cb512e commit e0cde27
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use rust_i18n::i18n;
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() {
i18n!("locales", fallback = "en");
match get_language() {
Some(lang) => {
rust_i18n::set_locale(lang.as_str());
Expand All @@ -11,12 +13,17 @@ pub fn set_locale() {
}
}
}

fn get_language() -> Option<String> {
match std::env::var("LANG") {
Ok(lang) => {
let language = lang.split('.').next();
language.map(|s| s.to_string())
let language = lang.split('.').next()?.to_string();
if SUPPORTED_LANGUAGES.contains(&language.as_str()) {
Some(language)
} else {
None
}
}
Err(_) => None,
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use i18n::set_locale;
mod i18n;
rust_i18n::i18n!("locales");
#[derive(Parser, Debug)]
#[clap(version = "0.0.1", author = "Fankai liu <[email protected]>")]
#[clap(version = "0.1.17", author = "Fankai liu <[email protected]>")]
struct Opts {
#[clap(subcommand)]
subcmd: SubCommand,
Expand Down

0 comments on commit e0cde27

Please sign in to comment.