Closed Steven-WZH closed 3 months ago
Hello, @Steven-WZH.
I think it's more efficient to utilize a single script rather than multiple scripts in different languages for install.sh
for maintaining a single source of truth.
Would you mind to create a function that prompts the user to select their preferred language, allowing for dynamic retrieval of text? Here's an example of what I have in mind:
SCRIPT_LANG="EN"
# Available Languages
# EN => English
# ZH => Chinese
EN[000]="\n Language:\n 1. English (Default) \n 2. 简体中文 (Chinese)\n"
ZH[000]="${EN[0000]}"
EN[001]="Choose:"
ZH[001]="请选择:"
function get_text() {
eval echo "\${${SCRIPT_LANG}[$*]}"
}
function prompt_language() {
local selected_lang="${SCRIPT_LANG}"
echo -e "${yellow} $(get_text 000) ${plain}"
read -rep "${yellow} $(get_text 001) ${plain}" "selected_lang"
case "${selected_lang}" in
1) SCRIPT_LANG="EN";;
2) SCRIPT_LANG="ZH";;
*) SCRIPT_LANG="EN";;
esac
}
prompt_language
Hello @hamid-gh98 Thanks for suggestion,your idea is good . I'll try to modify the script.
I have an another solution here.I'm not sure if it's proper for the install script. @hamid-gh98 @MHSanaei
#!/bin/bash
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
cur_dir=$(pwd)
language="English"
select_language() {
echo -e "Language:"
echo -e "1. English (Default)"
echo -e "2. 简体中文 (Chinese)"
read -p "Choose [1/2]: " lang_choice
if [ "$lang_choice" == "1" ]; then
echo "Selected language: English"
language="English"
elif [ "$lang_choice" == "2" ]; then
echo "选择的语言:简体中文"
language="Chinese"
#elif [ "$lang_choice" == "3" ]; then
# echo "Lan:Test"
# language="Test"
else
echo "Invalid selection, defaulting to English"
fi
}
select_language
echo_msg() {
if [ "$language" == "English" ]; then
echo -e "$1"
elif [ "$language" == "Chinese" ]; then
echo -e "$2"
#elif [ "$language" == "Test" ]; then
# echo -e "$3"
fi
}
# Example usage of echo_msg function
echo_msg "Lan EN 1" "Lan ZH 2" #"Lan test 3"
I have an another solution here.I'm not sure if it's proper for the install script. @hamid-gh98 @MHSanaei
Example usage of echo_msg function
echo_msg "Lan EN 1" "Lan ZH 2" #"Lan test 3"
maybe in future we have also other languages, its better to have a separated variable for each language like before rather than pass all messages in a single line.
Sorry for not explaining clearly.It's actually sparated here.
echo_msg() {
if [ "$language" == "English" ]; then
echo -e "$1"
elif [ "$language" == "Chinese" ]; then
echo -e "$2"
#elif [ "$language" == "Test" ]; then
# echo -e "$3"
fi
}
But I think the code above is not friendly for multi-language compatibility.Here is my new idea and it's maybe more easy to support different languages
#!/bin/bash
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
cur_dir=$(pwd)
language="English"
# choose
select_language() {
echo -e "Language/语言:"
echo -e "1. English (默认/Default)"
echo -e "2. 简体中文 (Chinese)"
read -p "Choose/请选择 [1/2]: " lang_choice
if [ "$lang_choice" == "1" ]; then
echo "Selected language: English"
language="English"
elif [ "$lang_choice" == "2" ]; then
echo "选择的语言:简体中文"
language="Chinese"
else
echo "Invalid selection, defaulting to English/无效选择,默认为英语"
fi
}
# language selection
select_language
# echo messages based on language selection
echo_msg() {
local message_key="$1"
if [ "$language" == "English" ]; then
echo -e "${messages_en[$message_key]}"
elif [ "$language" == "Chinese" ]; then
echo -e "${messages_zh[$message_key]}"
fi
}
# Define
declare -A messages_en
messages_en['welcome']="Welcome to the installation script!"
messages_en['install_started']="Installation process started."
declare -A messages_zh
messages_zh['welcome']="欢迎使用安装脚本!"
messages_zh['install_started']="安装过程已启动。"
# examples
echo_msg 'welcome'
echo_msg 'install_started'
But I think the code above is not friendly for multi-language compatibility.Here is my new idea and it's maybe more easy to support different languages
# choose select_language() { echo -e "Language/语言:" echo -e "1. English (默认/Default)" echo -e "2. 简体中文 (Chinese)" read -p "Choose/请选择 [1/2]: " lang_choice if [ "$lang_choice" == "1" ]; then echo "Selected language: English" language="English" elif [ "$lang_choice" == "2" ]; then echo "选择的语言:简体中文" language="Chinese" else echo "Invalid selection, defaulting to English/无效选择,默认为英语" fi } # language selection select_language # echo messages based on language selection echo_msg() { local message_key="$1" if [ "$language" == "English" ]; then echo -e "${messages_en[$message_key]}" elif [ "$language" == "Chinese" ]; then echo -e "${messages_zh[$message_key]}" fi } # Define declare -A messages_en messages_en['welcome']="Welcome to the installation script!" messages_en['install_started']="Installation process started." declare -A messages_zh messages_zh['welcome']="欢迎使用安装脚本!" messages_zh['install_started']="安装过程已启动。" echo_msg 'welcome' echo_msg 'install_started'
yeah, that way is much better. thanks
The install.sh is not important if you want to translate anything do it for x-ui.sh
supported chinese install script