nidhal baccouri commited on
Commit
37d3379
·
1 Parent(s): d3c12a3

added cli class

Browse files
deep_translator/__main__.py CHANGED
@@ -15,10 +15,10 @@ def main():
15
  '-trans',
16
  default='google',
17
  type=str,
18
- help="name of the translator you want to use",
19
- required=True)
20
  parser.add_argument('--source',
21
  '-src',
 
22
  type=str,
23
  help="source language to translate from")
24
  parser.add_argument('--target',
@@ -37,11 +37,11 @@ def main():
37
 
38
  args = parser.parse_args()
39
 
40
- cli = CLI()
41
  if args.languages:
42
- cli.get_supported_languages(args)
43
  else:
44
- cli.translate(args)
45
 
46
 
47
  if __name__ == "__main__":
 
15
  '-trans',
16
  default='google',
17
  type=str,
18
+ help="name of the translator you want to use")
 
19
  parser.add_argument('--source',
20
  '-src',
21
+ default='auto',
22
  type=str,
23
  help="source language to translate from")
24
  parser.add_argument('--target',
 
37
 
38
  args = parser.parse_args()
39
 
40
+ cli = CLI(args)
41
  if args.languages:
42
+ cli.get_supported_languages()
43
  else:
44
+ cli.translate()
45
 
46
 
47
  if __name__ == "__main__":
deep_translator/cli.py CHANGED
@@ -16,8 +16,9 @@ from .libre import LibreTranslator
16
  class CLI(object):
17
 
18
  translators_dict = None
 
19
 
20
- def __init__(self):
21
  self.translators_dict = {
22
  'google': GoogleTranslator,
23
  'pons': PonsTranslator,
@@ -30,32 +31,32 @@ class CLI(object):
30
  'qcri': QCRI,
31
  'papago': PapagoTranslator
32
  }
33
-
34
- def _get_translator(self, name):
35
- return self.translators_dict[name]
36
-
37
- def translate(self, args):
 
 
 
38
  """
39
  function used to provide translations from the parsed terminal arguments
40
- @param args: parsed terminal arguments
41
  @return: None
42
  """
43
- translator = self._get_translator(args.translator)
44
- res = translator.translate(args.text)
45
- print(" | Translation from {} to {} |".format(args.source, args.target))
46
- print("Translated text: \n {}".format(res))
47
 
48
- def get_supported_languages(self, args):
49
  """
50
  function used to return the languages supported by the translator service from the parsed terminal arguments
51
- @param args: parsed terminal arguments
52
  @return: None
53
  """
54
- translator = self._get_translator(args.translator)
55
- translator_supported_languages = translator.get_supported_languages(as_dict=True)
56
- print(f'Languages supported by \'{args.translator}\' are :\n')
57
  print(translator_supported_languages)
58
- sys.exit()
59
 
60
 
61
 
 
16
  class CLI(object):
17
 
18
  translators_dict = None
19
+ translator = None
20
 
21
+ def __init__(self, args):
22
  self.translators_dict = {
23
  'google': GoogleTranslator,
24
  'pons': PonsTranslator,
 
31
  'qcri': QCRI,
32
  'papago': PapagoTranslator
33
  }
34
+ self.args = args
35
+ translator_class = self.translators_dict.get(self.args.translator, None)
36
+ if not translator_class:
37
+ raise Exception(f"Translator {self.args.translator} is not supported."
38
+ f"Supported translators: {list(self.translators_dict.keys())}")
39
+ self.translator = translator_class(source=args.source, target=args.target)
40
+
41
+ def translate(self):
42
  """
43
  function used to provide translations from the parsed terminal arguments
 
44
  @return: None
45
  """
46
+ res = self.translator.translate(self.args.text)
47
+ print("Translation from {} to {}".format(self.args.source, self.args.target))
48
+ print("-"*50)
49
+ print("Translation result: {}".format(res))
50
 
51
+ def get_supported_languages(self):
52
  """
53
  function used to return the languages supported by the translator service from the parsed terminal arguments
 
54
  @return: None
55
  """
56
+
57
+ translator_supported_languages = self.translator.get_supported_languages(as_dict=True)
58
+ print(f'Languages supported by \'{self.args.translator}\' are :\n')
59
  print(translator_supported_languages)
 
60
 
61
 
62
 
docs/README.rst CHANGED
@@ -156,7 +156,11 @@ or even directly from terminal:
156
 
157
  .. code-block:: console
158
 
159
- $ deep-translator translate -src "en" -tgt "de" -txt "keep it up, you are awesome"
 
 
 
 
160
 
161
 
162
  =====
@@ -534,7 +538,7 @@ Libre Translator
534
  .. note::
535
 
536
  Libre translate has multiple `mirrors <https://github.com/LibreTranslate/LibreTranslate#mirrors>`_ which can be used for the API endpoint.
537
- Some require an API key to be used. By default the base url is set to `libretranslate.de <https://libretranslate.de/>`_ .
538
  This can be set using the "base_url" input parameter.
539
 
540
  .. code-block:: python
@@ -565,8 +569,8 @@ Libre Translator
565
  .. code-block:: python
566
 
567
  translated = LibreTranslator(source='auto', target='en').translate_file('path/to/file')
568
-
569
-
570
 
571
  Proxy usage
572
  -------------
 
156
 
157
  .. code-block:: console
158
 
159
+ $ deep-translator -src "en" -tg "de" -txt "hello world"
160
+
161
+ or shorter
162
+
163
+ $ dt -tg de -txt "hello world"
164
 
165
 
166
  =====
 
538
  .. note::
539
 
540
  Libre translate has multiple `mirrors <https://github.com/LibreTranslate/LibreTranslate#mirrors>`_ which can be used for the API endpoint.
541
+ Some require an API key to be used. By default the base url is set to `libretranslate.de <https://libretranslate.de/>`_ .
542
  This can be set using the "base_url" input parameter.
543
 
544
  .. code-block:: python
 
569
  .. code-block:: python
570
 
571
  translated = LibreTranslator(source='auto', target='en').translate_file('path/to/file')
572
+
573
+
574
 
575
  Proxy usage
576
  -------------