2015-06-05 16:43:05 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import argparse
|
2015-07-17 13:57:20 +02:00
|
|
|
import sys
|
2017-08-08 13:42:45 +02:00
|
|
|
from googletrans import Translator
|
2015-06-05 16:43:05 +02:00
|
|
|
|
|
|
|
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)')
|
2015-06-06 07:44:15 +02:00
|
|
|
parser.add_argument('-c', '--detect', action='store_true', default=False,
|
|
|
|
help='')
|
2015-06-05 16:43:05 +02:00
|
|
|
args = parser.parse_args()
|
2017-08-08 13:42:45 +02:00
|
|
|
translator = Translator()
|
2015-06-05 16:43:05 +02:00
|
|
|
|
2015-06-06 07:44:15 +02:00
|
|
|
if args.detect:
|
|
|
|
result = translator.detect(args.text)
|
|
|
|
result = """
|
|
|
|
[{lang}, {confidence}] {text}
|
|
|
|
""".strip().format(text=args.text,
|
|
|
|
lang=result.lang, confidence=result.confidence)
|
|
|
|
print(result)
|
|
|
|
return
|
|
|
|
|
2015-06-05 16:43:05 +02:00
|
|
|
result = translator.translate(args.text, dest=args.dest, src=args.src)
|
2015-07-20 17:59:40 +02:00
|
|
|
result = u"""
|
2015-06-05 16:43:05 +02:00
|
|
|
[{src}] {original}
|
|
|
|
->
|
|
|
|
[{dest}] {text}
|
|
|
|
[pron.] {pronunciation}
|
2017-08-08 13:42:45 +02:00
|
|
|
""".strip().format(src=result.src, dest=result.dest, original=result.origin,
|
2015-07-20 17:59:40 +02:00
|
|
|
text=result.text, pronunciation=result.pronunciation)
|
2015-06-05 16:43:05 +02:00
|
|
|
print(result)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|