This commit is contained in:
paniy
2025-01-15 23:34:14 +08:00
parent aa857d380e
commit 2badb57614
3 changed files with 89 additions and 57 deletions
+15 -1
View File
@@ -9,12 +9,22 @@ on:
- 'translate.py'
branches:
- main
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
update-translations:
runs-on: ubuntu-latest
steps:
- name: Check running workflows
uses: styfle/cancel-workflow-action@0.12.1
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v4
- name: Restore translation cache
@@ -70,7 +80,7 @@ jobs:
- name: Save translation cache
if: always()
uses: actions/cache/save@v3
uses: actions/cache@v3
with:
path: po/cache_*.json
key: ${{ runner.os }}-translations-${{ hashFiles('po/*.po') }}
@@ -88,6 +98,10 @@ jobs:
echo "$TIMESTAMP" > languages/${lang}/LC_MESSAGES/version
done
- name: Pull changes
run: |
git pull origin main --rebase
- name: Commit changes
run: |
git config --local user.email "action@github.com"
+26 -26
View File
@@ -3279,33 +3279,33 @@ list() {
}
show_help() {
echo "$(gettext "usage: idleleo [OPTION]")"
echo "usage: idleleo [OPTION]"
echo
echo "$(gettext "OPTION:")"
echo "$(gettext " -1, --install-tls 安装 Xray (Nginx+ws/gRPC+TLS)")"
echo "$(gettext " -2, --install-reality 安装 Xray (Nginx+Reality+ws/gRPC)")"
echo "$(gettext " -3, --install-none 安装 Xray (ws/gRPC ONLY)")"
echo "$(gettext " -4, --add-upstream 变更 Nginx 负载均衡配置")"
echo "$(gettext " -5, --add-servernames 变更 Nginx serverNames 配置")"
echo "$(gettext " -au, --auto-update 设置自动更新")"
echo "$(gettext " -c, --clean-logs 清除日志文件")"
echo "$(gettext " -cs, --cert-status 查看证书状态")"
echo "$(gettext " -cu, --cert-update 更新证书有效期")"
echo "$(gettext " -cau, --cert-auto-update 设置证书自动更新")"
echo "$(gettext " -f, --set-fail2ban 设置 Fail2ban 防暴力破解")"
echo "$(gettext " -h, --help 显示帮助")"
echo "$(gettext " -n, --nginx-update 更新 Nginx")"
echo "$(gettext " -p, --port-reset 变更 port")"
echo "$(gettext " --purge, --uninstall 脚本卸载")"
echo "$(gettext " -s, --show 显示安装信息")"
echo "$(gettext " -t, --target-reset 变更 target")"
echo "$(gettext " -tcp, --tcp 配置 TCP 加速")"
echo "$(gettext " -tls, --tls 修改 TLS 配置")"
echo "$(gettext " -u, --update 升级脚本")"
echo "$(gettext " -uu, --uuid-reset 变更 UUIDv5/映射字符串")"
echo "$(gettext " -xa, --xray-access 显示 Xray 访问信息")"
echo "$(gettext " -xe, --xray-error 显示 Xray 错误信息")"
echo "$(gettext " -x, --xray-update 更新 Xray")"
echo "OPTION:"
echo " -1, --install-tls $(gettext "安装 Xray") (Nginx+ws/gRPC+TLS)"
echo " -2, --install-reality $(gettext "安装 Xray") (Nginx+Reality+ws/gRPC)"
echo " -3, --install-none $(gettext "安装 Xray") (ws/gRPC ONLY)"
echo " -4, --add-upstream $(gettext "变更 Nginx 负载均衡配置")"
echo " -5, --add-servernames $(gettext "变更 Nginx serverNames 配置")"
echo " -au, --auto-update $(gettext "设置自动更新")"
echo " -c, --clean-logs $(gettext "清除日志文件")"
echo " -cs, --cert-status $(gettext "查看证书状态")"
echo " -cu, --cert-update $(gettext "更新证书有效期")"
echo " -cau, --cert-auto-update $(gettext "设置证书自动更新")"
echo " -f, --set-fail2ban $(gettext "设置 Fail2ban 防暴力破解")"
echo " -h, --help $(gettext "显示帮助")"
echo " -n, --nginx-update $(gettext "更新") Nginx"
echo " -p, --port-reset $(gettext "变更") port"
echo " --purge, --uninstall $(gettext "脚本卸载")"
echo " -s, --show $(gettext "显示安装信息")"
echo " -t, --target-reset $(gettext "变更") target"
echo " -tcp, --tcp $(gettext "配置 TCP 加速")"
echo " -tls, --tls $(gettext "修改 TLS 配置")"
echo " -u, --update $(gettext "升级脚本")"
echo " -uu, --uuid-reset $(gettext "变更") UUIDv5/$(gettext "映射字符串")"
echo " -xa, --xray-access $(gettext "显示") Xray $(gettext "访问信息")"
echo " -xe, --xray-error $(gettext "显示") Xray $(gettext "错误信息")"
echo " -x, --xray-update $(gettext "更新") Xray"
exit 0
}
+48 -30
View File
@@ -3,6 +3,7 @@ import time
import json
import os
from googletrans import Translator, LANGUAGES
import concurrent.futures
def load_translation_cache(cache_file):
if os.path.exists(cache_file):
@@ -14,53 +15,70 @@ def save_translation_cache(cache_file, translations):
with open(cache_file, 'w', encoding='utf-8') as f:
json.dump(translations, f, ensure_ascii=False, indent=2)
def batch_translate(translator, texts, src_lang, dest_lang):
translations = {}
max_retries = 3
with concurrent.futures.ThreadPoolExecutor() as executor:
future_to_text = {executor.submit(translate_with_retry, translator, text, src_lang, dest_lang, max_retries): text for text in texts}
for future in concurrent.futures.as_completed(future_to_text):
text = future_to_text[future]
try:
translation = future.result()
translations[text] = translation
except Exception as e:
print(f"Translation failed for: {text}")
print(f"Error: {e}")
return translations
def translate_with_retry(translator, text, src_lang, dest_lang, max_retries):
for attempt in range(max_retries):
try:
time.sleep(2) # 增加延迟以避免请求过快
translation = translator.translate(text, src=src_lang, dest=dest_lang)
return translation.text
except Exception as e:
if attempt == max_retries - 1:
raise e
print(f"Retry {attempt + 1}/{max_retries} for: {text}")
time.sleep(5) # 重试前等待更长时间
def translate_po_file(input_file, output_file, target_lang):
cache_file = f'po/cache_{target_lang}.json'
translations = load_translation_cache(cache_file)
# 使用最新的 Translator,并设置更可靠的服务 URL
translator = Translator(service_urls=['translate.google.com'])
with open(input_file, 'r', encoding='utf-8') as f:
content = f.read()
pattern = r'msgid "(.*?)"\nmsgstr ""'
pattern = r'msgid "(.*?)"\nmsgstr "(.*?)"'
matches = re.finditer(pattern, content)
updated = False
texts_to_translate = []
for match in matches:
chinese_text = match.group(1)
if chinese_text and any('\u4e00' <= char <= '\u9fff' for char in chinese_text):
if chinese_text in translations:
translated_text = translations[chinese_text]
print(f"Using cached translation [{target_lang}]: {chinese_text} -> {translated_text}")
else:
try:
# 增加重试机制
max_retries = 3
for attempt in range(max_retries):
try:
time.sleep(2) # 增加延迟以避免请求过快
translation = translator.translate(chinese_text, src='zh-cn', dest=target_lang)
translated_text = translation.text
translations[chinese_text] = translated_text
updated = True
print(f"New translation [{target_lang}]: {chinese_text} -> {translated_text}")
break
except Exception as e:
if attempt == max_retries - 1:
raise e
print(f"Retry {attempt + 1}/{max_retries} for: {chinese_text}")
time.sleep(5) # 重试前等待更长时间
except Exception as e:
print(f"Translation failed for: {chinese_text}")
print(f"Error: {e}")
continue
if chinese_text not in translations:
texts_to_translate.append(chinese_text)
if texts_to_translate:
new_translations = batch_translate(translator, texts_to_translate, 'zh-cn', target_lang)
translations.update(new_translations)
updated = True
else:
updated = False
for match in matches:
chinese_text = match.group(1)
existing_translation = match.group(2)
translated_text = translations.get(chinese_text, existing_translation)
if existing_translation != translated_text:
content = content.replace(
f'msgid "{chinese_text}"\nmsgstr ""',
f'msgid "{chinese_text}"\nmsgstr "{existing_translation}"',
f'msgid "{chinese_text}"\nmsgstr "{translated_text}"'
)
updated = True
if updated:
save_translation_cache(cache_file, translations)