mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-11-23 20:11:07 +00:00
Add TranslateCLI (#200)
This commit is contained in:
parent
341935f471
commit
cf21e68e6b
|
@ -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)
|
|[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)
|
|[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)|
|
|[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 :
|
## How to use :
|
||||||
|
|
14
TranslateCLI/README.md
Normal file
14
TranslateCLI/README.md
Normal 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
35
TranslateCLI/Translate.py
Normal 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)
|
1
TranslateCLI/requirements.txt
Normal file
1
TranslateCLI/requirements.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
googletrans
|
Loading…
Reference in New Issue
Block a user