From 453dc1d8e0bdcf1af2ebbafc6ffecd02e6cd2c31 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 10 Nov 2025 09:24:32 +0800 Subject: Add JV_LANG environment variable support and improve CLI structure --- scripts/cli.sh | 25 +++++++++++++++++++++---- src/utils/env.rs | 28 ++++++++++++++++++++++++++++ src/utils/lang_selector.rs | 14 -------------- 3 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 src/utils/env.rs delete mode 100644 src/utils/lang_selector.rs diff --git a/scripts/cli.sh b/scripts/cli.sh index ba21c54..454bd6f 100644 --- a/scripts/cli.sh +++ b/scripts/cli.sh @@ -3,12 +3,26 @@ # Get the real directory where this script is located SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" -# Aliases -alias jvh='jv here' +############## +### CONFIG ### +############## + +# Use JV_LANG to set CLI language +# Supported: en, zh-CN +# export JV_LANG = en + +############### +### ALIASES ### +############### + +alias jj='jv here' alias jvu='jv update' alias jvt='jv track' -# Completion +################## +### COMPLETION ### +################## + if [ -f "$SCRIPT_DIR/completion_jv.sh" ]; then source "$SCRIPT_DIR/completion_jv.sh" fi @@ -16,7 +30,10 @@ if [ -f "$SCRIPT_DIR/completion_jvv.sh" ]; then source "$SCRIPT_DIR/completion_jvv.sh" fi -# Add bin directory to PATH +################## +### ENVIREMENT ### +################## + if [ -d "$SCRIPT_DIR/bin" ]; then export PATH="$SCRIPT_DIR/bin:$PATH" fi diff --git a/src/utils/env.rs b/src/utils/env.rs new file mode 100644 index 0000000..028995e --- /dev/null +++ b/src/utils/env.rs @@ -0,0 +1,28 @@ +// /// Returns the current locale string based on environment variables. +/// +/// The function checks for locale settings in the following order: +/// 1. JV_LANG environment variable +/// 2. APP_LANG environment variable +/// 3. LANG environment variable (extracts base language before dot and replaces underscores with hyphens) +/// 4. Defaults to "en" if no locale environment variables are found +/// +/// # Returns +/// A String containing the detected locale code +pub fn current_locales() -> String { + if let Ok(lang) = std::env::var("JV_LANG") { + return lang; + } + + if let Ok(lang) = std::env::var("APP_LANG") { + return lang; + } + + if let Ok(lang) = std::env::var("LANG") { + if let Some(base_lang) = lang.split('.').next() { + return base_lang.replace('_', "-"); + } + return lang; + } + + "en".to_string() +} diff --git a/src/utils/lang_selector.rs b/src/utils/lang_selector.rs deleted file mode 100644 index c3603ea..0000000 --- a/src/utils/lang_selector.rs +++ /dev/null @@ -1,14 +0,0 @@ -pub fn current_locales() -> String { - if let Ok(lang) = std::env::var("APP_LANG") { - return lang; - } - - if let Ok(lang) = std::env::var("LANG") { - if let Some(base_lang) = lang.split('.').next() { - return base_lang.replace('_', "-"); - } - return lang; - } - - "en".to_string() -} -- cgit