diff --git a/src/i18n.rs b/src/i18n.rs index c4a9158..4cae160 100644 --- a/src/i18n.rs +++ b/src/i18n.rs @@ -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()); @@ -11,12 +13,17 @@ pub fn set_locale() { } } } + fn get_language() -> Option { 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, } -} +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0100c71..0bf8643 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 ")] +#[clap(version = "0.1.17", author = "Fankai liu ")] struct Opts { #[clap(subcommand)] subcmd: SubCommand,