自动化国际化脚本

This commit is contained in:
XieHaoLiang 2024-07-25 11:27:52 +08:00
parent e7745d945b
commit e22d3fbe99

View File

@ -2,13 +2,30 @@
#主语言包
MAIN_LANGUAGE_PATH="C:\Users\xhj\Desktop\lan_en.json"
#写入路径
WRITE_LANGUAGE_PATH="C:\Users\xhj\Desktop\lan_tw.json"
MAIN_LANGUAGE_PATH="lan_en.json"
#语言包存储前缀路径
BASE_PATH="./lan"
#原语言
SOURCE_LANGUAGE="en"
#目标语言
TARGET_LANGUAGE="zh-tw"
#项目语言常量
LAN_ZH_CN="zh_CN"
LAN_ZH_TW="zh_TW"
LAN_EN_US="en_US"
declare -A LANG_LIST=(
["$LAN_ZH_CN"]="简体中文"
["$LAN_ZH_TW"]="繁体中文"
["$LAN_EN_US"]="英文"
)
# 项目语言映射到阿里云 阿里云API语言代码请看https://help.aliyun.com/zh/machine-translation/support/supported-languages-and-codes?spm=a2c4g.11186623.0.0.42484f4fJXlVRF
declare -A ALIYUN_LANG_MAP=(
["$LAN_ZH_CN"]="zh"
["$LAN_ZH_TW"]="zh-tw"
["$LAN_EN_US"]="en"
)
#===============以下代码请勿改动,除非知道怎么修复=============================
@ -26,16 +43,13 @@ SIGNATURE_METHOD='HMAC-SHA1'
SIGNATURE_VERSION='1.0'
# 执行自动翻译
autoTranslate() {
local en_us_path="$MAIN_LANGUAGE_PATH"
local en_us_path="$BASE_PATH/$MAIN_LANGUAGE_PATH"
local en_us_data=$(cat "$en_us_path" | jq -c '.')
local path="$WRITE_LANGUAGE_PATH"
local data=$(cat "$path" | jq -c '.')
local total=$(echo "$en_us_data" | jq 'length')
local current=0
# 创建临时文件
local tmpfile=$(mktemp)
@ -43,36 +57,53 @@ autoTranslate() {
# 将JSON数据写入临时文件
echo "$en_us_data" | jq -r 'to_entries | .[] | "\(.key) \(.value)"' > "$tmpfile"
# 遍历JSON中的每个键值对
while read -r key value; do
current=$((current + 1))
echo "Processing item $current/$total)"
# 如果翻译已经存在,则跳过
if echo "$data" | jq -e --arg key "$key" '.[$key]' > /dev/null; then
continue
fi
# 循环遍历列表中的每种语言
for lang in "${!LANG_LIST[@]}"; do
# 过滤特定的语言
if [ "$lang" == "en_US" ]; then
continue
fi
# 尝试翻译这个值
translation=$(translate "$SOURCE_LANGUAGE" "$TARGET_LANGUAGE" "$value" )
#检测返回值是否ERROR
if [[ "$translation" == "ERROR" ]]; then
echo "Failed to translate: $key"
continue
fi
local path="$BASE_PATH/lan_${ALIYUN_LANG_MAP[$lang]}.json"
# 如果文件不存在则跳过即创建并且默认写入json
if [ ! -f "$path" ]; then
echo "{}" > "$path"
fi
local data=$(cat "$path" | jq -c '.')
local total=$(echo "$en_us_data" | jq 'length')
local current=0
if [ -n "$translation" ]; then
#向data中添加key-value
data=$(echo "$data" | jq --arg key "$key" --arg translation "$translation" '. + {($key): $translation}')
fi
done < "$tmpfile" #读取临时文件
# 遍历JSON中的每个键值对
while read -r key value; do
current=$((current + 1))
echo "Processing item $current/$total)"
# 如果翻译已经存在,则跳过
if echo "$data" | jq -e --arg key "$key" '.[$key]' > /dev/null; then
continue
fi
# 尝试翻译这个值
translation=$(translate "$SOURCE_LANGUAGE" "${ALIYUN_LANG_MAP[$lang]}" "$value" )
#检测返回值是否ERROR
if [[ "$translation" == "ERROR" ]]; then
echo "Failed to translate: $key"
continue
fi
if [ -n "$translation" ]; then
#向data中添加key-value
data=$(echo "$data" | jq --arg key "$key" --arg translation "$translation" '. + {($key): $translation}')
fi
done < "$tmpfile" #读取临时文件
#将更新后的翻译写回文件
echo "$data" | jq '.' > "$path"
done
#将更新后的翻译写回文件
echo "$data" | jq '.' > "$path"
# Remove the temporary file
rm "$tmpfile"
}