Created
January 21, 2022 17:47
-
-
Save shdwkl/40b3e83ed9e66faf9205ef370fb0049b to your computer and use it in GitHub Desktop.
Translator From any Language into arabic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import logging | |
| from pathlib import Path | |
| from string import Template | |
| import click | |
| from googletrans import Translator | |
| ROOT_DIR = Path(__file__).resolve(strict=True).parent | |
| LOGS_DIR = ROOT_DIR / "logs" | |
| LOGS_DIR.mkdir(parents=True, exist_ok=True) | |
| source_language = "auto" | |
| destination_language = "ar" | |
| LINE_SEPERATION = "\n\n" + 70 * "=" | |
| TRANSLATION_TEMPLATE = Template(""" | |
| word no.$index | |
| $not_arabic_word | |
| . | |
| .. | |
| ... | |
| $arabic_word | |
| """) | |
| logging.basicConfig( | |
| format="%(asctime)s %(message)s", | |
| datefmt="%m/%d/%Y %I:%M:%S %p", | |
| filename= str(LOGS_DIR / "translations.log"), | |
| level=logging.INFO, | |
| ) | |
| @click.command() | |
| @click.option( | |
| "--words", | |
| "-w", | |
| multiple=True, | |
| help="Translation Into Arabic", | |
| ) | |
| def trans(words): | |
| """Translator From any Language into arabic. | |
| :param words: words to translate | |
| :type words: str | |
| """ | |
| translator = Translator() | |
| for i, word in enumerate(words): | |
| res = translator.translate(word,src=source_language ,dest=destination_language) | |
| buffer = TRANSLATION_TEMPLATE.safe_substitute(index=i+1, not_arabic_word=word, arabic_word=res.text) | |
| log_translations(buffer) | |
| click.echo(f"\n\n{res.text:25}") | |
| seperate_translations() | |
| def seperate_translations(): | |
| logging.info(LINE_SEPERATION) | |
| def log_translations(translation): | |
| logging.info(translation) | |
| if __name__ == "__main__": | |
| trans() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment