Update translate.py
This commit is contained in:
+30
-48
@@ -3,7 +3,6 @@ import time
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from googletrans import Translator, LANGUAGES
|
from googletrans import Translator, LANGUAGES
|
||||||
import concurrent.futures
|
|
||||||
|
|
||||||
def load_translation_cache(cache_file):
|
def load_translation_cache(cache_file):
|
||||||
if os.path.exists(cache_file):
|
if os.path.exists(cache_file):
|
||||||
@@ -15,70 +14,53 @@ def save_translation_cache(cache_file, translations):
|
|||||||
with open(cache_file, 'w', encoding='utf-8') as f:
|
with open(cache_file, 'w', encoding='utf-8') as f:
|
||||||
json.dump(translations, f, ensure_ascii=False, indent=2)
|
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):
|
def translate_po_file(input_file, output_file, target_lang):
|
||||||
cache_file = f'po/cache_{target_lang}.json'
|
cache_file = f'po/cache_{target_lang}.json'
|
||||||
translations = load_translation_cache(cache_file)
|
translations = load_translation_cache(cache_file)
|
||||||
|
|
||||||
|
# 使用最新的 Translator,并设置更可靠的服务 URL
|
||||||
translator = Translator(service_urls=['translate.google.com'])
|
translator = Translator(service_urls=['translate.google.com'])
|
||||||
|
|
||||||
with open(input_file, 'r', encoding='utf-8') as f:
|
with open(input_file, 'r', encoding='utf-8') as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
|
||||||
pattern = r'msgid "(.*?)"\nmsgstr "(.*?)"'
|
pattern = r'msgid "(.*?)"\nmsgstr ""'
|
||||||
matches = re.finditer(pattern, content)
|
matches = re.finditer(pattern, content)
|
||||||
|
|
||||||
texts_to_translate = []
|
updated = False
|
||||||
for match in matches:
|
for match in matches:
|
||||||
chinese_text = match.group(1)
|
chinese_text = match.group(1)
|
||||||
if chinese_text and any('\u4e00' <= char <= '\u9fff' for char in chinese_text):
|
if chinese_text and any('\u4e00' <= char <= '\u9fff' for char in chinese_text):
|
||||||
if chinese_text not in translations:
|
if chinese_text in translations:
|
||||||
texts_to_translate.append(chinese_text)
|
translated_text = translations[chinese_text]
|
||||||
|
print(f"Using cached translation [{target_lang}]: {chinese_text} -> {translated_text}")
|
||||||
if texts_to_translate:
|
else:
|
||||||
new_translations = batch_translate(translator, texts_to_translate, 'zh-cn', target_lang)
|
try:
|
||||||
translations.update(new_translations)
|
# 增加重试机制
|
||||||
updated = True
|
max_retries = 3
|
||||||
else:
|
for attempt in range(max_retries):
|
||||||
updated = False
|
try:
|
||||||
|
time.sleep(2) # 增加延迟以避免请求过快
|
||||||
for match in matches:
|
translation = translator.translate(chinese_text, src='zh-cn', dest=target_lang)
|
||||||
chinese_text = match.group(1)
|
translated_text = translation.text
|
||||||
existing_translation = match.group(2)
|
translations[chinese_text] = translated_text
|
||||||
translated_text = translations.get(chinese_text, existing_translation)
|
updated = True
|
||||||
|
print(f"New translation [{target_lang}]: {chinese_text} -> {translated_text}")
|
||||||
if existing_translation != 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
|
||||||
|
|
||||||
content = content.replace(
|
content = content.replace(
|
||||||
f'msgid "{chinese_text}"\nmsgstr "{existing_translation}"',
|
f'msgid "{chinese_text}"\nmsgstr ""',
|
||||||
f'msgid "{chinese_text}"\nmsgstr "{translated_text}"'
|
f'msgid "{chinese_text}"\nmsgstr "{translated_text}"'
|
||||||
)
|
)
|
||||||
updated = True
|
|
||||||
|
|
||||||
if updated:
|
if updated:
|
||||||
save_translation_cache(cache_file, translations)
|
save_translation_cache(cache_file, translations)
|
||||||
|
|||||||
Reference in New Issue
Block a user