-
Notifications
You must be signed in to change notification settings - Fork 729
/
translate
executable file
·47 lines (40 loc) · 1.18 KB
/
translate
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
#!/usr/bin/env python
import argparse
from googletrans import Translator
def main():
parser = argparse.ArgumentParser(
description="Python Google Translator as a command-line tool"
)
parser.add_argument("text", help="The text you want to translate.")
parser.add_argument(
"-d",
"--dest",
default="en",
help="The destination language you want to translate. (Default: en)",
)
parser.add_argument(
"-s",
"--src",
default="auto",
help="The source language you want to translate. (Default: auto)",
)
parser.add_argument("-c", "--detect", action="store_true", default=False, help="")
args = parser.parse_args()
translator = Translator()
if args.detect:
result = translator.detect(args.text)
result = f"""
[{result.lang}, {result.confidence}] {args.text}
""".strip()
print(result)
return
result = translator.translate(args.text, dest=args.dest, src=args.src)
result = f"""
[{result.src}] {result.origin}
->
[{result.dest}] {result.text}
[pron.] {result.pronunciation}
""".strip()
print(result)
if __name__ == "__main__":
main()