Chris
commited on
Commit
·
279b84e
1
Parent(s):
92cc291
more refactoring (and a minor setup.py tweak)
Browse files- README.rst +2 -2
- deep_translator/__init__.py +11 -9
- deep_translator/cli.py +69 -71
- deep_translator/tests/test_cli.py +3 -3
- setup.py +3 -2
README.rst
CHANGED
|
@@ -647,7 +647,7 @@ Next Steps
|
|
| 647 |
|
| 648 |
Take a look in the examples folder for more :)
|
| 649 |
Contributions are always welcome.
|
| 650 |
-
Read the Contribution
|
| 651 |
|
| 652 |
==========
|
| 653 |
Credits
|
|
@@ -673,7 +673,7 @@ The Translator++ mobile app
|
|
| 673 |
:alt: Icon of the app
|
| 674 |
|
| 675 |
|
| 676 |
-
After developing the deep_translator, I
|
| 677 |
Sure, there is google translate, pons and linguee apps etc.. but isn't it cooler to make an app where all these
|
| 678 |
translators are integrated?
|
| 679 |
|
|
|
|
| 647 |
|
| 648 |
Take a look in the examples folder for more :)
|
| 649 |
Contributions are always welcome.
|
| 650 |
+
Read the Contribution guidelines `Here <https://deep-translator.readthedocs.io/en/latest/contributing.html/>`_
|
| 651 |
|
| 652 |
==========
|
| 653 |
Credits
|
|
|
|
| 673 |
:alt: Icon of the app
|
| 674 |
|
| 675 |
|
| 676 |
+
After developing the deep_translator, I realized how cool this would be if I can use it as an app on my mobile phone.
|
| 677 |
Sure, there is google translate, pons and linguee apps etc.. but isn't it cooler to make an app where all these
|
| 678 |
translators are integrated?
|
| 679 |
|
deep_translator/__init__.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
"""Top-level package for deep_translator."""
|
| 2 |
|
| 3 |
-
from .google_trans import GoogleTranslator
|
| 4 |
-
from .pons import PonsTranslator
|
| 5 |
-
from .linguee import LingueeTranslator
|
| 6 |
-
from .mymemory import MyMemoryTranslator
|
| 7 |
-
from .yandex import YandexTranslator
|
| 8 |
-
from .qcri import QCRI
|
| 9 |
-
from .deepl import DeepL
|
| 10 |
-
from .detection import single_detection, batch_detection
|
| 11 |
-
from .microsoft import MicrosoftTranslator
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
__author__ = """Nidhal Baccouri"""
|
|
@@ -23,5 +24,6 @@ __all__ = [GoogleTranslator,
|
|
| 23 |
MicrosoftTranslator,
|
| 24 |
QCRI,
|
| 25 |
DeepL,
|
|
|
|
| 26 |
single_detection,
|
| 27 |
batch_detection]
|
|
|
|
| 1 |
"""Top-level package for deep_translator."""
|
| 2 |
|
| 3 |
+
from deep_translator.google_trans import GoogleTranslator
|
| 4 |
+
from deep_translator.pons import PonsTranslator
|
| 5 |
+
from deep_translator.linguee import LingueeTranslator
|
| 6 |
+
from deep_translator.mymemory import MyMemoryTranslator
|
| 7 |
+
from deep_translator.yandex import YandexTranslator
|
| 8 |
+
from deep_translator.qcri import QCRI
|
| 9 |
+
from deep_translator.deepl import DeepL
|
| 10 |
+
from deep_translator.detection import single_detection, batch_detection
|
| 11 |
+
from deep_translator.microsoft import MicrosoftTranslator
|
| 12 |
+
from deep_translator.cli import main
|
| 13 |
|
| 14 |
|
| 15 |
__author__ = """Nidhal Baccouri"""
|
|
|
|
| 24 |
MicrosoftTranslator,
|
| 25 |
QCRI,
|
| 26 |
DeepL,
|
| 27 |
+
main,
|
| 28 |
single_detection,
|
| 29 |
batch_detection]
|
deep_translator/cli.py
CHANGED
|
@@ -13,6 +13,75 @@ from papago import PapagoTranslator
|
|
| 13 |
|
| 14 |
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def translate(translator, source, target, text, api_key):
|
| 18 |
"""
|
|
@@ -65,7 +134,6 @@ def translate(translator, source, target, text, api_key):
|
|
| 65 |
click.echo(f" | Translation from {source} to {target} |")
|
| 66 |
click.echo(f"Translated text: \n {res}")
|
| 67 |
|
| 68 |
-
|
| 69 |
def print_supported_languages(requested_translator, api_key):
|
| 70 |
"""
|
| 71 |
function used to print the languages supported by the translator service
|
|
@@ -102,75 +170,5 @@ def print_supported_languages(requested_translator, api_key):
|
|
| 102 |
for k, v in supported_languages.items():
|
| 103 |
click.echo(f"|- {k}: {v}")
|
| 104 |
|
| 105 |
-
|
| 106 |
-
@click.command(context_settings=CONTEXT_SETTINGS)
|
| 107 |
-
# @click.option(
|
| 108 |
-
# "--translator",
|
| 109 |
-
# "-trans",
|
| 110 |
-
# default="google",
|
| 111 |
-
# type=str,
|
| 112 |
-
# help="name of the translator you want to use",
|
| 113 |
-
# show_default=True,
|
| 114 |
-
# )
|
| 115 |
-
|
| 116 |
-
@click.argument(
|
| 117 |
-
'translator',
|
| 118 |
-
required=True,
|
| 119 |
-
default='google',
|
| 120 |
-
type=str,
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
@click.option(
|
| 124 |
-
"--source",
|
| 125 |
-
"-src",
|
| 126 |
-
required=True
|
| 127 |
-
type=str,
|
| 128 |
-
help="source language to translate from"
|
| 129 |
-
)
|
| 130 |
-
@click.option(
|
| 131 |
-
"--target",
|
| 132 |
-
"-tg",
|
| 133 |
-
required=True
|
| 134 |
-
type=str,
|
| 135 |
-
help="target language to translate to"
|
| 136 |
-
)
|
| 137 |
-
@click.option(
|
| 138 |
-
"--text",
|
| 139 |
-
"-txt",
|
| 140 |
-
type=str,
|
| 141 |
-
help="text you want to translate"
|
| 142 |
-
)
|
| 143 |
-
@click.option(
|
| 144 |
-
"--api-key",
|
| 145 |
-
type=str,
|
| 146 |
-
help="required for DeepL, QCRI, Yandex, Microsoft and Papago translators"
|
| 147 |
-
)
|
| 148 |
-
@click.option(
|
| 149 |
-
"--languages",
|
| 150 |
-
"-lang",
|
| 151 |
-
is_flag=True,
|
| 152 |
-
help="list all the languages available with the translator."
|
| 153 |
-
" Run with deep_translator -trans <translator service> -lang",
|
| 154 |
-
)
|
| 155 |
-
def main(translator, source, target, text, api_key, languages):
|
| 156 |
-
"""
|
| 157 |
-
Use TRANSLATOR to translate source material into another language.
|
| 158 |
-
Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.
|
| 159 |
-
\f
|
| 160 |
-
function responsible for parsing terminal arguments and provide them for
|
| 161 |
-
further use in the translation process
|
| 162 |
-
"""
|
| 163 |
-
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
|
| 164 |
-
if translator in api_key_required and not api_key:
|
| 165 |
-
click.echo(
|
| 166 |
-
"This translator requires an api key provided through --api-key"
|
| 167 |
-
)
|
| 168 |
-
elif languages:
|
| 169 |
-
print_supported_languages(translator, api_key)
|
| 170 |
-
else:
|
| 171 |
-
translate(translator, source, target, text, api_key)
|
| 172 |
-
# sys.exit()
|
| 173 |
-
|
| 174 |
-
|
| 175 |
if __name__ == "__main__":
|
| 176 |
main()
|
|
|
|
| 13 |
|
| 14 |
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
| 15 |
|
| 16 |
+
def main(translator, source, target, text, api_key, languages):
|
| 17 |
+
"""
|
| 18 |
+
Use TRANSLATOR to translate source material into another language.\n
|
| 19 |
+
Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.\n
|
| 20 |
+
\n
|
| 21 |
+
\f
|
| 22 |
+
function responsible for parsing terminal arguments and provide them for
|
| 23 |
+
further use in the translation process
|
| 24 |
+
"""
|
| 25 |
+
api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
|
| 26 |
+
if translator in api_key_required and not api_key:
|
| 27 |
+
click.echo(
|
| 28 |
+
"This translator requires an api key provided through --api-key"
|
| 29 |
+
)
|
| 30 |
+
elif languages:
|
| 31 |
+
print_supported_languages(translator, api_key)
|
| 32 |
+
else:
|
| 33 |
+
translate(translator, source, target, text, api_key)
|
| 34 |
+
# sys.exit()
|
| 35 |
+
|
| 36 |
+
@click.command(context_settings=CONTEXT_SETTINGS)
|
| 37 |
+
# @click.option(
|
| 38 |
+
# "--translator",
|
| 39 |
+
# "-trans",
|
| 40 |
+
# default="google",
|
| 41 |
+
# type=str,
|
| 42 |
+
# help="name of the translator you want to use",
|
| 43 |
+
# show_default=True,
|
| 44 |
+
# )
|
| 45 |
+
|
| 46 |
+
@click.argument(
|
| 47 |
+
'translator',
|
| 48 |
+
required=True,
|
| 49 |
+
default='google',
|
| 50 |
+
type=str,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
@click.option(
|
| 54 |
+
"--source",
|
| 55 |
+
"-src",
|
| 56 |
+
required=True,
|
| 57 |
+
type=str,
|
| 58 |
+
help="source language to translate from"
|
| 59 |
+
)
|
| 60 |
+
@click.option(
|
| 61 |
+
"--target",
|
| 62 |
+
"-tgt",
|
| 63 |
+
required=True,
|
| 64 |
+
type=str,
|
| 65 |
+
help="target language to translate to"
|
| 66 |
+
)
|
| 67 |
+
@click.option(
|
| 68 |
+
"--text",
|
| 69 |
+
"-txt",
|
| 70 |
+
type=str,
|
| 71 |
+
help="text you want to translate"
|
| 72 |
+
)
|
| 73 |
+
@click.option(
|
| 74 |
+
"--api-key",
|
| 75 |
+
type=str,
|
| 76 |
+
help="required for DeepL, QCRI, Yandex, Microsoft and Papago translators"
|
| 77 |
+
)
|
| 78 |
+
@click.option(
|
| 79 |
+
"--languages",
|
| 80 |
+
"-lang",
|
| 81 |
+
is_flag=True,
|
| 82 |
+
help="list all the languages available with the translator."
|
| 83 |
+
" Run with deep_translator <translator service> -lang",
|
| 84 |
+
)
|
| 85 |
|
| 86 |
def translate(translator, source, target, text, api_key):
|
| 87 |
"""
|
|
|
|
| 134 |
click.echo(f" | Translation from {source} to {target} |")
|
| 135 |
click.echo(f"Translated text: \n {res}")
|
| 136 |
|
|
|
|
| 137 |
def print_supported_languages(requested_translator, api_key):
|
| 138 |
"""
|
| 139 |
function used to print the languages supported by the translator service
|
|
|
|
| 170 |
for k, v in supported_languages.items():
|
| 171 |
click.echo(f"|- {k}: {v}")
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
if __name__ == "__main__":
|
| 174 |
main()
|
deep_translator/tests/test_cli.py
CHANGED
|
@@ -3,10 +3,10 @@
|
|
| 3 |
"""Tests for `deep_translator` package."""
|
| 4 |
|
| 5 |
from click.testing import CliRunner
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
def
|
| 9 |
runner = CliRunner()
|
| 10 |
-
result = runner.invoke(cli.
|
| 11 |
assert result.exit_code == 0
|
| 12 |
assert result == 'good'
|
|
|
|
| 3 |
"""Tests for `deep_translator` package."""
|
| 4 |
|
| 5 |
from click.testing import CliRunner
|
| 6 |
+
from deep_translator.cli import main
|
| 7 |
|
| 8 |
+
def results_test():
|
| 9 |
runner = CliRunner()
|
| 10 |
+
result = runner.invoke(cli.main, [ 'google', 'auto', 'en', '좋은'])
|
| 11 |
assert result.exit_code == 0
|
| 12 |
assert result == 'good'
|
setup.py
CHANGED
|
@@ -47,10 +47,11 @@ setup(
|
|
| 47 |
description="A flexible python tool to translate between different languages in a simple way.",
|
| 48 |
entry_points={
|
| 49 |
'console_scripts': [
|
| 50 |
-
'deep_translator=
|
|
|
|
| 51 |
],
|
| 52 |
},
|
| 53 |
-
install_requires=['requests', 'beautifulsoup4'],
|
| 54 |
license="MIT license",
|
| 55 |
long_description=readme + '\n\n' + history,
|
| 56 |
include_package_data=True,
|
|
|
|
| 47 |
description="A flexible python tool to translate between different languages in a simple way.",
|
| 48 |
entry_points={
|
| 49 |
'console_scripts': [
|
| 50 |
+
'deep_translator = cli:main',
|
| 51 |
+
'dt = cli:main',
|
| 52 |
],
|
| 53 |
},
|
| 54 |
+
install_requires=['requests', 'beautifulsoup4', 'Click'],
|
| 55 |
license="MIT license",
|
| 56 |
long_description=readme + '\n\n' + history,
|
| 57 |
include_package_data=True,
|