From e0cde27e109f6b76316d9c436f032e519898b0cc Mon Sep 17 00:00:00 2001 From: Fankai Liu Date: Tue, 7 Nov 2023 19:35:08 +0800 Subject: [PATCH] fix bug i18n --- src/i18n.rs | 17 ++++++++++++----- src/main.rs | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) 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,