Merge pull request #49 from bigsbunny/extend-cli-arguments
Browse filesAdded functionality for -lang/--languages flag to the CLI usage to return the languages supported by the chosen translator service.
- README.rst +7 -3
- deep_translator/cli.py +30 -5
README.rst
CHANGED
@@ -486,8 +486,7 @@ the right arguments, which are the translator you want to use, source language,
|
|
486 |
you want to translate.
|
487 |
|
488 |
For example, provide "google" as an argument to use the google translator. Alternatively you can use
|
489 |
-
the other supported translators. Just read the documentation to have an overview about the supported
|
490 |
-
translators in this library.
|
491 |
|
492 |
.. code-block:: console
|
493 |
|
@@ -505,6 +504,12 @@ If you want, you can also pass the source and target language by their abbreviat
|
|
505 |
|
506 |
$ deep_translator -trans "google" -src "en" -tg "de" -txt "happy coding"
|
507 |
|
|
|
|
|
|
|
|
|
|
|
|
|
508 |
======
|
509 |
Tests
|
510 |
======
|
@@ -619,4 +624,3 @@ Here are some screenshots:
|
|
619 |
:width: 100%
|
620 |
:height: 300
|
621 |
:alt: screenshot3
|
622 |
-
|
|
|
486 |
you want to translate.
|
487 |
|
488 |
For example, provide "google" as an argument to use the google translator. Alternatively you can use
|
489 |
+
the other supported translators. Just read the documentation to have an overview about the supported translators in this library.
|
|
|
490 |
|
491 |
.. code-block:: console
|
492 |
|
|
|
504 |
|
505 |
$ deep_translator -trans "google" -src "en" -tg "de" -txt "happy coding"
|
506 |
|
507 |
+
If you want the list of languages supported by a translator service, just pass the translator service as an argument to the -trans flag followed by the -lang/--languages flag. For example:
|
508 |
+
|
509 |
+
.. code-block:: console
|
510 |
+
|
511 |
+
$ deep_translator -trans "google" -lang
|
512 |
+
|
513 |
======
|
514 |
Tests
|
515 |
======
|
|
|
624 |
:width: 100%
|
625 |
:height: 300
|
626 |
:alt: screenshot3
|
|
deep_translator/cli.py
CHANGED
@@ -30,6 +30,27 @@ def translate(args):
|
|
30 |
print(" | Translation from {} to {} |".format(args.source, args.target))
|
31 |
print("Translated text: \n {}".format(res))
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def main():
|
35 |
"""
|
@@ -38,13 +59,17 @@ def main():
|
|
38 |
"""
|
39 |
parser = argparse.ArgumentParser()
|
40 |
parser.add_argument('--translator', '-trans',
|
41 |
-
default='google', type=str, help="name of the translator you want to use")
|
42 |
-
parser.add_argument('--source', '-src', type=str, help="source language to translate from"
|
43 |
-
parser.add_argument('--target', '-tg', type=str, help="target language to translate to"
|
44 |
-
parser.add_argument('--text', '-txt', type=str, help="text you want to translate"
|
|
|
45 |
|
46 |
args = parser.parse_args()
|
47 |
-
|
|
|
|
|
|
|
48 |
# sys.exit()
|
49 |
|
50 |
|
|
|
30 |
print(" | Translation from {} to {} |".format(args.source, args.target))
|
31 |
print("Translated text: \n {}".format(res))
|
32 |
|
33 |
+
def return_supported_languages(args):
|
34 |
+
"""
|
35 |
+
function used to return the languages supported by the translator service from the parsed terminal arguments
|
36 |
+
@param args: parsed terminal arguments
|
37 |
+
@return: None
|
38 |
+
"""
|
39 |
+
if args.translator == 'google':
|
40 |
+
translator = GoogleTranslator
|
41 |
+
elif args.translator == 'pons':
|
42 |
+
translator = PonsTranslator
|
43 |
+
elif args.translator == 'linguee':
|
44 |
+
translator = LingueeTranslator
|
45 |
+
elif args.translator == 'mymemory':
|
46 |
+
translator = MyMemoryTranslator
|
47 |
+
else:
|
48 |
+
print("given translator is not supported. Please use a supported translator from the deep_translator tool")
|
49 |
+
|
50 |
+
translator_supported_languages = translator.get_supported_languages(as_dict=True)
|
51 |
+
print(f'Languages supported by \'{args.translator}\' are :\n')
|
52 |
+
print(translator_supported_languages)
|
53 |
+
|
54 |
|
55 |
def main():
|
56 |
"""
|
|
|
59 |
"""
|
60 |
parser = argparse.ArgumentParser()
|
61 |
parser.add_argument('--translator', '-trans',
|
62 |
+
default='google', type=str, help="name of the translator you want to use", required=True)
|
63 |
+
parser.add_argument('--source', '-src', type=str, help="source language to translate from")
|
64 |
+
parser.add_argument('--target', '-tg', type=str, help="target language to translate to")
|
65 |
+
parser.add_argument('--text', '-txt', type=str, help="text you want to translate")
|
66 |
+
parser.add_argument('--languages', '-lang',action='store_true', help="all the languages available with the translator. Run the command deep_translator -trans <translator service> -lang")
|
67 |
|
68 |
args = parser.parse_args()
|
69 |
+
if args.languages:
|
70 |
+
return_supported_languages(args)
|
71 |
+
else:
|
72 |
+
translate(args)
|
73 |
# sys.exit()
|
74 |
|
75 |
|