shell 增加国际化

This commit is contained in:
whyour
2026-06-14 16:45:53 +08:00
parent 0ee0b83207
commit b5a5fb3be6
16 changed files with 420 additions and 94 deletions
+38 -17
View File
@@ -80,6 +80,15 @@ import_config() {
[[ -f $file_config_user ]] && . $file_config_user
[[ -f $dir_preload/lang_env.sh ]] && . $dir_preload/lang_env.sh
# 加载语言包(bash 4+ 支持 declare -A,不兼容时回退输出中文 key)
local lang=${QL_LANG:-zh}
local lang_file="$dir_shell/lang/${lang}.sh"
if [[ ${BASH_VERSINFO[0]} -ge 4 ]] && [[ -f $lang_file ]]; then
. $lang_file
elif [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
. "$dir_shell/lang/zh.sh"
fi
load_ql_envs
command_timeout_time=${CommandTimeoutTime:-""}
file_extensions=${RepoFileExtensions:-"js py"}
@@ -92,6 +101,18 @@ import_config() {
fi
}
t() {
local key="$1"
shift
local msg
if declare -p LANG_MESSAGES &>/dev/null; then
msg="${LANG_MESSAGES["$key"]}"
fi
[[ -z $msg ]] && msg="$key"
# shellcheck disable=SC2059
printf "$msg\n" "$@"
}
set_proxy() {
local proxy="$1"
if [[ $proxy ]]; then
@@ -226,7 +247,7 @@ npm_install_2() {
local dir_work=$1
cd $dir_work
echo -e "安装 $dir_work 依赖包...\n"
t '安装 %s 依赖包...\n' "$dir_work"
npm_install_sub
cd $dir_current
}
@@ -245,7 +266,7 @@ git_clone_scripts() {
local branch="$3"
local proxy="$4"
[[ $branch ]] && local part_cmd="-b $branch "
echo -e "开始拉取仓库 ${uniq_path}$dir\n"
t '开始拉取仓库 %s 到 %s\n' "${uniq_path}" "$dir"
set_proxy "$proxy"
@@ -278,18 +299,18 @@ reload_pm2() {
return 0
else
local exit_code=$?
echo "警告: PM2 启动失败 (退出码: $exit_code),可能是由于硬件不兼容"
echo "正在尝试直接使用 Node.js 启动服务..."
t '警告: PM2 启动失败 (退出码: %s),可能是由于硬件不兼容' "$exit_code"
t '正在尝试直接使用 Node.js 启动服务...'
# Kill any existing node processes for qinglong
pkill -f "node.*static/build/app.js" 2>/dev/null || true
# Start node directly in the background
nohup node static/build/app.js > $dir_log/qinglong.log 2>&1 &
local node_pid=$!
echo "已使用 Node.js 直接启动服务 (PID: $node_pid)"
echo "注意: 使用此模式时,部分 PM2 管理功能将不可用"
t '已使用 Node.js 直接启动服务 (PID: %s)' "$node_pid"
t '注意: 使用此模式时,部分 PM2 管理功能将不可用'
return 0
fi
}
@@ -361,16 +382,16 @@ handle_task_start() {
error_message=", 任务状态更新失败(${error})"
fi
fi
echo -e "## 开始执行... ${begin_time}${error_message}\n"
t '## 开始执行... %s\n' "${begin_time}${error_message}"
}
run_task_before() {
. $file_task_before "$@"
if [[ ${task_before:=} ]]; then
echo -e "执行前置命令\n"
t '执行前置命令\n'
eval "${task_before%;}"
echo -e "\n执行前置命令结束\n"
t '\n执行前置命令结束\n'
fi
}
@@ -378,9 +399,9 @@ run_task_after() {
. $file_task_after "$@"
if [[ ${task_after:=} ]]; then
echo -e "\n执行后置命令\n"
t '\n执行后置命令\n'
eval "${task_after%;}"
echo -e "\n执行后置命令结束"
t '\n执行后置命令结束'
fi
}
@@ -402,11 +423,11 @@ handle_task_end() {
record_cron_stat "$ID" "${exit_code:-0}" "$diff_time"
if [[ "${MANUAL:=}" == "true" ]]; then
echo -e "\n## 已停止 🛑... $end_time 耗时 $diff_time${error_message:=}     "
t '\n## 已停止 🛑... %s 耗时 %s 秒%s' "$end_time" "$diff_time" "${error_message:=}     "
elif [[ $exit_code -eq 0 ]]; then
echo -e "\n## 完成 ✅... $end_time 耗时 $diff_time${error_message:=}     "
t '\n## 完成 ✅... %s 耗时 %s 秒%s' "$end_time" "$diff_time" "${error_message:=}     "
else
echo -e "\n## 失败 ❌(退出码 ${exit_code})... $end_time 耗时 $diff_time${error_message:=}     "
t '\n## 失败 ❌(退出码 %s)... %s 耗时 %s 秒%s' "$exit_code" "$end_time" "$diff_time" "${error_message:=}     "
fi
}