-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
66 lines (52 loc) · 1.8 KB
/
translate.py
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
import os
import sys
from googletrans import Translator
SRC_LANG = 'zh-cn'
DEST_LANG = 'en'
def read_file(input_file):
with open(input_file, 'r', encoding='utf-8') as f:
return f.readlines()
def write_file(output_file, lines):
with open(output_file, 'w', encoding='utf-8') as f:
f.writelines("\r\n".join(lines))
def build_output_filepath(input_file):
root, ext = os.path.splitext(input_file)
return f"{root}_{DEST_LANG}{ext}"
def try_translate(content):
translator = Translator(
service_urls=['translate.google.com',
'translate.google.co.kr'],
)
res = []
for idx, line in enumerate(content):
if line in ['\n', '\r\n'] or line.strip() == "":
res.append(line)
continue
try:
trans = translator.translate(
line, src=SRC_LANG, dest=DEST_LANG).text
except Exception as e:
print(f"Error occurred during translate {idx}: {e}")
res.append(line)
else:
res.append(trans)
return res
if __name__ == "__main__":
n_param = len(sys.argv)
if n_param < 2 or n_param > 4:
print(
f"Usage: python {sys.argv[0]} <file_path> [<src_lang>] [<dest_lang>]")
sys.exit(1)
input_file_path = sys.argv[1]
if n_param >= 3:
SRC_LANG = sys.argv[2]
if n_param >= 4:
SRC_LANG = sys.argv[3]
output_file_path = build_output_filepath(input_file_path)
content = read_file(input_file_path)
translated_content = try_translate(content)
if isinstance(translated_content, list)\
and len(translated_content) == len(content):
write_file(output_file_path, translated_content)
print(
f"Translation completed. The result has been saved to: {output_file_path}")