forked from LizardByte/Sunshine
-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathtranslate_simple.py
More file actions
78 lines (69 loc) · 2.7 KB
/
Copy pathtranslate_simple.py
File metadata and controls
78 lines (69 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import re
import requests
# 项目名和术语保护列表
PROTECTED_TERMS = [
'Sunshine', 'README', 'GitHub', 'CI', 'API', 'Markdown', 'OpenAI', 'DeepL', 'Google Translate',
# 可在此添加更多术语
]
def mask_terms(text):
for term in PROTECTED_TERMS:
text = re.sub(rf'(?<![`\w]){re.escape(term)}(?![`\w])', f'@@@{term}@@@', text)
return text
def unmask_terms(text):
for term in PROTECTED_TERMS:
text = text.replace(f'@@@{term}@@@', term)
return text
def translate_with_deepseek(text, target_lang):
# 使用 DeepSeek API 进行翻译
api_key = os.getenv('DEEPSEEK_API_KEY')
if not api_key:
raise Exception('DEEPSEEK_API_KEY 环境变量未设置')
url = 'https://api.deepseek.com/v1/chat/completions'
prompt = f"请将以下 Markdown 内容翻译为{target_lang},但不要翻译项目名和术语:{', '.join(PROTECTED_TERMS)}。保持原有格式、链接和图片。\n\n{text}"
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
payload = {
"model": "deepseek-chat",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.2
}
resp = requests.post(url, headers=headers, json=payload)
resp.raise_for_status()
result = resp.json()
return result['choices'][0]['message']['content']
def translate_readme():
with open('README.md', 'r', encoding='utf-8') as f:
content = f.read()
languages = [
('en', 'English'),
('fr', 'French'),
('de', 'German'),
('ja', 'Japanese')
]
for lang_code, lang_name in languages:
try:
if lang_code == 'zh_CN':
translated_content = content
else:
masked = mask_terms(content)
translated = translate_with_deepseek(masked, lang_name)
translated = unmask_terms(translated)
# 去除 DeepSeek 返回的多余提示,只保留第一个 Markdown 标题及后面内容
lines = translated.splitlines()
for idx, line in enumerate(lines):
if line.strip().startswith('#'):
translated_content = '\n'.join(lines[idx:])
break
else:
translated_content = translated.strip()
filename = f'README.{lang_code}.md'
with open(filename, 'w', encoding='utf-8') as f:
f.write(translated_content)
print(f"✓ Translated to {lang_name} ({lang_code})")
except Exception as e:
print(f"✗ Failed to translate to {lang_name}: {e}")
if __name__ == "__main__":
translate_readme()