File size: 4,833 Bytes
3e848bc f36740b da3c3c8 810451b bb7c731 da3c3c8 41742b2 bb7c731 41742b2 5ef2374 9fa6116 fdb2846 5cbd28e 41742b2 fdb2846 41742b2 fdb2846 41742b2 da3c3c8 5ef2374 da3c3c8 5ef2374 da3c3c8 5ef2374 bb7c731 41742b2 da3c3c8 41742b2 bb7c731 41742b2 b96c7cf 5ef2374 41742b2 5ef2374 b96c7cf 41742b2 b96c7cf 41742b2 b96c7cf 41742b2 da3c3c8 41742b2 da3c3c8 41742b2 da3c3c8 41742b2 da3c3c8 41742b2 da3c3c8 41742b2 da3c3c8 b96c7cf 41742b2 b96c7cf da3c3c8 41742b2 da3c3c8 41742b2 b96c7cf 3e848bc 674f1bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
"""Console script for deep_translator."""
import click
from .google_trans import GoogleTranslator
from .mymemory import MyMemoryTranslator
from .deepl import DeepL
from .qcri import QCRI
from .linguee import LingueeTranslator
from .pons import PonsTranslator
from .yandex import YandexTranslator
from .microsoft import MicrosoftTranslator
from .papago import PapagoTranslator
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
@click.group()
def cli():
pass
@cli.command(context_settings=CONTEXT_SETTINGS, no_args_is_help=True)
@click.argument('translator', required=True, default='google', type=str)
@click.option("--source", "-src", required=True, type=str, help="source language to translate from")
@click.option("--target", "-tgt", required=True, type=str, help="target language to translate to")
@click.option("--text", "-txt", type=str,required = True,prompt="Enter the text you want to translate",help="text you want to translate")
@click.option("--api-key",type=str,help="required for DeepL, QCRI, Yandex, Microsoft and Papago translators")
def translate(translator, source, target, text, api_key):
"""
Use TRANSLATOR to translate source material into another language.
\f
Directory function to send arguments to the correct translator.
@param translator: translator name parsed from terminal arguments
@return: None
"""
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
if translator in api_key_required and not api_key:
click.echo(
"This translator requires an api key provided through --api-key")
else:
pass
if translator == "google":
translator = GoogleTranslator(source=source, target=target)
elif translator == "mymemory":
translator = MyMemoryTranslator(source=source, target=target)
elif translator == "deepl":
translator = DeepL(source=source, target=target, api_key=api_key)
elif translator == "qcri":
translator = QCRI(source=source, target=target, api_key=api_key)
elif translator == "linguee":
translator = LingueeTranslator(source=source, target=target)
elif translator == "pons":
translator = PonsTranslator(source=source, target=target)
elif translator == "yandex":
translator = YandexTranslator(
source=source,
target=target,
api_key=api_key)
elif translator == "microsoft":
translator = MicrosoftTranslator(
source=source,
target=target,
api_key=api_key)
elif translator == "papago":
translator = PapagoTranslator(
source=source,
target=target,
api_key=api_key)
else:
raise AttributeError("The given translator is not supported.")
res = translator.translate(text)
click.echo(f" | Translation from {source} to {target} |")
click.echo(f"Translated text: \n {res}")
return 0
@cli.command(context_settings=CONTEXT_SETTINGS, no_args_is_help=True)
@click.argument('translator')
@click.argument('api_key', required=False)
def languages(translator, api_key):
"""
Retrieve the list of available languages from the given translator.
@param translator: Translator given by the user.
@param api_key: Optional API key given by the user. Required for some translators.
@return: None
"""
translator = translator.lower()
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
if translator in api_key_required and not api_key:
click.echo("This translator requires an api key provided through --api-key")
else:
pass
if translator == "google":
translator = GoogleTranslator
elif translator == "mymemory":
translator = MyMemoryTranslator
elif translator == "qcri":
translator = QCRI(api_key=api_key)
elif translator == "linguee":
translator = LingueeTranslator
elif translator == "pons":
translator = PonsTranslator
elif translator == "yandex":
translator = YandexTranslator(api_key=api_key)
elif translator == "microsoft":
translator = MicrosoftTranslator(api_key=api_key)
elif translator == "papago":
translator = PapagoTranslator(api_key=api_key)
else:
raise AttributeError("The given translator is not supported.")
supported_languages = translator.get_supported_languages(as_dict=True)
click.echo(f"Languages supported by '{translator}' are :")
for k, v in supported_languages.items():
click.echo(f"|- {k}: {v}")
return 0
@cli.command()
def list():
"""Lists available translators."""
click.echo("Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.")
return 0
if __name__ == "__main__":
cli()
|