自动化国际化脚本
This commit is contained in:
parent
a4e93f7794
commit
2bbf7ca613
160
translation.sh
Normal file
160
translation.sh
Normal file
@ -0,0 +1,160 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
#主语言包
|
||||
MAIN_LANGUAGE_PATH="C:\Users\xhj\Desktop\lan_en.json"
|
||||
#写入路径
|
||||
WRITE_LANGUAGE_PATH="C:\Users\xhj\Desktop\lan_tw.json"
|
||||
#原语言
|
||||
SOURCE_LANGUAGE="en"
|
||||
#目标语言
|
||||
TARGET_LANGUAGE="zh-tw"
|
||||
|
||||
|
||||
#===============以下代码请勿改动,除非知道怎么修复=============================
|
||||
|
||||
#阿里云KEYID
|
||||
ACCESS_KEY_ID="LTAI5tFke4yGaY94ondXkmHn"
|
||||
#阿里云KEYSECRET
|
||||
ACCESS_KEY_SECRET="08zPw8BoRqfqlKDTMX65k7gfeER33x"
|
||||
|
||||
DOMAIN='mt.cn-hangzhou.aliyuncs.com/api/translate/web/ecommerce'
|
||||
VERSION='2018-10-12'
|
||||
ACTION='TranslateGeneral'
|
||||
FORMAT='JSON'
|
||||
SIGNATURE_METHOD='HMAC-SHA1'
|
||||
SIGNATURE_VERSION='1.0'
|
||||
|
||||
|
||||
# 执行自动翻译
|
||||
autoTranslate() {
|
||||
local en_us_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)
|
||||
|
||||
# 将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
|
||||
|
||||
# 尝试翻译这个值
|
||||
translation=$(translate "$SOURCE_LANGUAGE" "$TARGET_LANGUAGE" "$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"
|
||||
|
||||
# Remove the temporary file
|
||||
rm "$tmpfile"
|
||||
}
|
||||
|
||||
|
||||
# 阿里云生成签名
|
||||
generate_signature() {
|
||||
local stringToSign=$1
|
||||
local secret="$2&"
|
||||
local signature=$(echo -n "$stringToSign" | openssl dgst -sha1 -binary -hmac "$secret" | base64)
|
||||
echo $(urlencode "$signature")
|
||||
}
|
||||
|
||||
# 阿里云发出API请求
|
||||
translate() {
|
||||
local accessKeyId=$ACCESS_KEY_ID
|
||||
local accessKeySecret=$ACCESS_KEY_SECRET
|
||||
local sourceLanguage=$1
|
||||
local targetLanguage=$2
|
||||
local sourceText=$3
|
||||
local method='POST'
|
||||
local timestamp=$(date -u "+%Y-%m-%dT%H:%M:%SZ")
|
||||
local signatureNonce=$(date -u "+%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
# Prepare API parameters
|
||||
declare -A apiParams
|
||||
apiParams=(
|
||||
["Action"]=$ACTION
|
||||
["Version"]=$VERSION
|
||||
["FormatType"]="text"
|
||||
["SourceLanguage"]=$sourceLanguage
|
||||
["TargetLanguage"]=$targetLanguage
|
||||
["SourceText"]=$sourceText
|
||||
["Ccene"]="general"
|
||||
["SignatureMethod"]=$SIGNATURE_METHOD
|
||||
["SignatureNonce"]=$signatureNonce
|
||||
["SignatureVersion"]=$SIGNATURE_VERSION
|
||||
["AccessKeyId"]=$accessKeyId
|
||||
["Timestamp"]=$timestamp
|
||||
["Format"]=$FORMAT
|
||||
)
|
||||
|
||||
# Sort parameters
|
||||
sortedKeys=$(printf "%s\n" "${!apiParams[@]}" | sort)
|
||||
sortedQueryString=""
|
||||
for key in $sortedKeys; do
|
||||
encodedKey=$(urlencode "$key")
|
||||
encodedValue=$(urlencode "${apiParams[$key]}")
|
||||
sortedQueryString="${sortedQueryString}&${encodedKey}=${encodedValue}"
|
||||
done
|
||||
sortedQueryString="${sortedQueryString:1}"
|
||||
|
||||
# Create the string to sign
|
||||
stringToSign="${method}&%2F&$(urlencode "$sortedQueryString")"
|
||||
|
||||
# Generate the signature
|
||||
signature=$(generate_signature "$stringToSign" "$accessKeySecret")
|
||||
|
||||
# Build the request URL
|
||||
requestUrl="https://${DOMAIN}/?Signature=${signature}&${sortedQueryString}"
|
||||
|
||||
# Make the API request using curl
|
||||
response=$(curl -s -X "$method" "$requestUrl" \
|
||||
-H "x-sdk-client: bash/1.0.0")
|
||||
|
||||
# Handle the response
|
||||
responseCode=$(echo "$response" | jq -r '.Code')
|
||||
if [[ "$responseCode" == "200" ]]; then
|
||||
echo $(echo "$response" | jq -r '.Data.Translated')
|
||||
else
|
||||
echo "ERROR"
|
||||
fi
|
||||
}
|
||||
|
||||
urlencode() {
|
||||
local length="${#1}"
|
||||
for ((i = 0; i < length; i++)); do
|
||||
local c="${1:i:1}"
|
||||
case $c in
|
||||
[a-zA-Z0-9.~_-]) printf "$c" ;;
|
||||
*) printf '%%%02X' "'$c" ;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
# Run
|
||||
autoTranslate
|
||||
Loading…
x
Reference in New Issue
Block a user