forked from aliajder/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_doc.py
42 lines (35 loc) · 1.57 KB
/
translate_doc.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
from googletrans import Translator
import argparse
import os
# init the translator
translator = Translator()
def translate(text, src="auto", dest="en"):
"""Translate `text` from `src` language to `dest`"""
return translator.translate(text, src=src, dest=dest).text
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simple Python script to translate text using Google Translate API (googletrans wrapper)")
parser.add_argument("target", help="Text/Document to translate")
parser.add_argument("-s", "--source", help="Source language, default is Google Translate's auto detection", default="auto")
parser.add_argument("-d", "--destination", help="Destination language, default is English", default="en")
args = parser.parse_args()
target = args.target
src = args.source
dest = args.destination
if os.path.isfile(target):
# translate a document instead
# get basename of file
basename = os.path.basename(target)
# get the path dir
dirname = os.path.dirname(target)
try:
filename, ext = basename.split(".")
except:
# no extension
filename = basename
ext = ""
translated_text = translate(open(target).read(), src=src, dest=dest)
# write to new document file
open(os.path.join(dirname, f"{filename}_{dest}{f'.{ext}' if ext else ''}"), "w").write(translated_text)
else:
# not a file, just text, print the translated text to standard output
print(translate(target, src=src, dest=dest))