|
1 | 1 | #!/usr/bin/env python
|
2 |
| -# -*- coding: utf-8 -*- |
3 | 2 | import argparse
|
4 |
| -import sys |
| 3 | + |
5 | 4 | from googletrans import Translator
|
6 | 5 |
|
| 6 | + |
7 | 7 | def main():
|
8 | 8 | parser = argparse.ArgumentParser(
|
9 |
| - description='Python Google Translator as a command-line tool') |
10 |
| - parser.add_argument('text', help='The text you want to translate.') |
11 |
| - parser.add_argument('-d', '--dest', default='en', |
12 |
| - help='The destination language you want to translate. (Default: en)') |
13 |
| - parser.add_argument('-s', '--src', default='auto', |
14 |
| - help='The source language you want to translate. (Default: auto)') |
15 |
| - parser.add_argument('-c', '--detect', action='store_true', default=False, |
16 |
| - help='') |
| 9 | + description="Python Google Translator as a command-line tool" |
| 10 | + ) |
| 11 | + parser.add_argument("text", help="The text you want to translate.") |
| 12 | + parser.add_argument( |
| 13 | + "-d", |
| 14 | + "--dest", |
| 15 | + default="en", |
| 16 | + help="The destination language you want to translate. (Default: en)", |
| 17 | + ) |
| 18 | + parser.add_argument( |
| 19 | + "-s", |
| 20 | + "--src", |
| 21 | + default="auto", |
| 22 | + help="The source language you want to translate. (Default: auto)", |
| 23 | + ) |
| 24 | + parser.add_argument("-c", "--detect", action="store_true", default=False, help="") |
17 | 25 | args = parser.parse_args()
|
18 | 26 | translator = Translator()
|
19 | 27 |
|
20 | 28 | if args.detect:
|
21 | 29 | result = translator.detect(args.text)
|
22 |
| - result = """ |
23 |
| -[{lang}, {confidence}] {text} |
24 |
| - """.strip().format(text=args.text, |
25 |
| - lang=result.lang, confidence=result.confidence) |
| 30 | + result = f""" |
| 31 | +[{result.lang}, {result.confidence}] {args.text} |
| 32 | + """.strip() |
26 | 33 | print(result)
|
27 | 34 | return
|
28 | 35 |
|
29 | 36 | result = translator.translate(args.text, dest=args.dest, src=args.src)
|
30 |
| - result = u""" |
31 |
| -[{src}] {original} |
| 37 | + result = f""" |
| 38 | +[{result.src}] {result.origin} |
32 | 39 | ->
|
33 |
| -[{dest}] {text} |
34 |
| -[pron.] {pronunciation} |
35 |
| - """.strip().format(src=result.src, dest=result.dest, original=result.origin, |
36 |
| - text=result.text, pronunciation=result.pronunciation) |
| 40 | +[{result.dest}] {result.text} |
| 41 | +[pron.] {result.pronunciation} |
| 42 | + """.strip() |
37 | 43 | print(result)
|
38 | 44 |
|
39 |
| -if __name__ == '__main__': |
| 45 | + |
| 46 | +if __name__ == "__main__": |
40 | 47 | main()
|
0 commit comments