Add TranslateCLI (#200)

This commit is contained in:
Rodrigo Oliveira 2020-10-28 06:49:15 -03:00 committed by GitHub
parent 341935f471
commit cf21e68e6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 0 deletions

View File

@ -169,6 +169,7 @@ So far, the following projects have been integrated to this repo:
|[Download Page as PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Download-page-as-pdf)|[Jeremias Gomes](https://github.com/j3r3mias)
|[Independent RSA Communication Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Communication)|[Miguel Santos](https://github.com/wi6n3l)
|[GithubBot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/GithubBot)|[Abhilasha](https://github.com/Abhilasha06)|
|[Translate CLI](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/TranslateCLI)|[Rodrigo Oliveira](https://github.com/rodrigocam)|
## How to use :

14
TranslateCLI/README.md Normal file
View File

@ -0,0 +1,14 @@
# Python Command Line Translator
Use google translate library to translate text from command line.
## Requirements
Python 3.xx
googletrans
```bash
pip install googletrans
```
### Usage
python Translate.py <text> -s <source_lang> -d <destination_lang>

35
TranslateCLI/Translate.py Normal file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env python3
import argparse
from googletrans import Translator
def translate(text, src_lng=None, dest_lng=None):
translator = Translator()
if src_lng and dest_lng:
translated = translator.translate(text, src=src_lng, dest=dest_lng)
elif src_lng:
translated = translator.translate(text, src=src_lng)
elif dest_lng:
translated = translator.translate(text, dest=dest_lng)
else:
translated = translator.translate(text)
return translated
parser = argparse.ArgumentParser()
parser.add_argument('text', type=str, help='text to translate')
parser.add_argument('-s', '--src', default=None, help='origin language of the text')
parser.add_argument('-d', '--dest', default=None, help='destiny language of the translation')
parser.add_argument('-v', '--verbose', help='show more information', action='store_true')
args = parser.parse_args()
tr = translate(args.text, args.src, args.dest)
if args.verbose:
print('original text: %s' % tr.origin)
print('translated text: %s' % tr.text)
print('origin language: %s' % tr.src)
print('destiny language: %s' % tr.dest)
else:
print(tr.text)

View File

@ -0,0 +1 @@
googletrans