= commited on
Commit
5a40797
·
1 Parent(s): 0e35eb5

fixed the auto detection for the mymemory translator

Browse files
README.rst CHANGED
@@ -96,12 +96,22 @@ take a look at the docs if you want to install from source.
96
  Usage
97
  =====
98
 
 
99
  .. code-block:: python
100
 
101
  from deep_translator import GoogleTranslator, PonsTranslator, LingueeTranslator, MyMemoryTranslator
102
 
103
  text = 'happy coding'
104
 
 
 
 
 
 
 
 
 
 
105
  - Simple translation:
106
 
107
  .. code-block:: python
@@ -152,8 +162,8 @@ Usage
152
 
153
  .. code-block:: python
154
 
155
- word = 'good'
156
- translated_word = MyMemoryTranslator(source='english', target='french').translate(word)
157
 
158
  Usage from Terminal
159
  ====================
 
96
  Usage
97
  =====
98
 
99
+
100
  .. code-block:: python
101
 
102
  from deep_translator import GoogleTranslator, PonsTranslator, LingueeTranslator, MyMemoryTranslator
103
 
104
  text = 'happy coding'
105
 
106
+
107
+ .. note::
108
+
109
+ You can check the supported languages of every translator by calling the
110
+ get_supported_languages function as a static method.
111
+
112
+ - Example of checking the supported languages for the google translator:
113
+
114
+
115
  - Simple translation:
116
 
117
  .. code-block:: python
 
162
 
163
  .. code-block:: python
164
 
165
+ word = 'Keep it up. You are awesome'
166
+ translated_word = MyMemoryTranslator(source='auto', target='french').translate(word)
167
 
168
  Usage from Terminal
169
  ====================
deep_translator/google_trans.py CHANGED
@@ -1,6 +1,6 @@
1
 
2
  from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
3
- from deep_translator.exceptions import LanguageNotSupportedException, ElementNotFoundInGetRequest, NotValidPayload, NotValidLength
4
  from deep_translator.parent import BaseTranslator
5
  from bs4 import BeautifulSoup
6
  import requests
@@ -10,7 +10,8 @@ class GoogleTranslator(BaseTranslator):
10
  """
11
  class that uses google translate to translate texts
12
  """
13
- supported_languages = list(GOOGLE_LANGUAGES_TO_CODES.keys())
 
14
 
15
  def __init__(self, source="auto", target="en"):
16
  """
@@ -31,6 +32,10 @@ class GoogleTranslator(BaseTranslator):
31
  hl=self._target,
32
  sl=self._source)
33
 
 
 
 
 
34
  def _map_language_to_code(self, *languages, **kwargs):
35
  """
36
 
 
1
 
2
  from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
3
+ from deep_translator.exceptions import LanguageNotSupportedException, ElementNotFoundInGetRequest, NotValidPayload
4
  from deep_translator.parent import BaseTranslator
5
  from bs4 import BeautifulSoup
6
  import requests
 
10
  """
11
  class that uses google translate to translate texts
12
  """
13
+ _languages = GOOGLE_LANGUAGES_TO_CODES
14
+ supported_languages = list(_languages.keys())
15
 
16
  def __init__(self, source="auto", target="en"):
17
  """
 
32
  hl=self._target,
33
  sl=self._source)
34
 
35
+ @staticmethod
36
+ def get_supported_languages(as_dict=False):
37
+ return GoogleTranslator.supported_languages if not as_dict else GoogleTranslator._languages
38
+
39
  def _map_language_to_code(self, *languages, **kwargs):
40
  """
41
 
deep_translator/linguee.py CHANGED
@@ -7,9 +7,10 @@ from requests.utils import requote_uri
7
 
8
 
9
  class LingueeTranslator(BaseTranslator):
10
- supported_languages = list(LINGUEE_LANGUAGES_TO_CODES.keys())
 
11
 
12
- def __init__(self, source, target):
13
  """
14
  @param source: source language to translate from
15
  @param target: target language to translate to
@@ -27,6 +28,10 @@ class LingueeTranslator(BaseTranslator):
27
  payload_key=None, # key of text in the url
28
  )
29
 
 
 
 
 
30
  def _map_language_to_code(self, *languages, **kwargs):
31
  """
32
  @param language: type of language
 
7
 
8
 
9
  class LingueeTranslator(BaseTranslator):
10
+ _languages = LINGUEE_LANGUAGES_TO_CODES
11
+ supported_languages = list(_languages.keys())
12
 
13
+ def __init__(self, source, target="en"):
14
  """
15
  @param source: source language to translate from
16
  @param target: target language to translate to
 
28
  payload_key=None, # key of text in the url
29
  )
30
 
31
+ @staticmethod
32
+ def get_supported_languages(as_dict=False):
33
+ return LingueeTranslator.supported_languages if not as_dict else LingueeTranslator._languages
34
+
35
  def _map_language_to_code(self, *languages, **kwargs):
36
  """
37
  @param language: type of language
deep_translator/mymemory.py CHANGED
@@ -9,15 +9,16 @@ class MyMemoryTranslator(BaseTranslator):
9
  """
10
  class that uses google translate to translate texts
11
  """
12
- supported_languages = list(GOOGLE_LANGUAGES_TO_CODES.keys())
 
13
 
14
- def __init__(self, source, target, **kwargs):
15
  """
16
  @param source: source language to translate from
17
  @param target: target language to translate to
18
  """
19
  self.__base_url = BASE_URLS.get("MYMEMORY")
20
- self._source = source if source != 'auto' else 'Autodetect'
21
  self._target = target
22
 
23
  self.email = kwargs.get('email', None)
@@ -27,6 +28,10 @@ class MyMemoryTranslator(BaseTranslator):
27
  payload_key='q',
28
  langpair='{}|{}'.format(self._source, self._target))
29
 
 
 
 
 
30
  def translate(self, text, **kwargs):
31
  """
32
  main function that uses google translate to translate a text
@@ -45,6 +50,7 @@ class MyMemoryTranslator(BaseTranslator):
45
  response = requests.get(self.__base_url,
46
  params=self._url_params,
47
  headers=self.headers)
 
48
  data = response.json()
49
  if not data:
50
  raise Exception("Translation was not found in response!")
@@ -81,3 +87,8 @@ class MyMemoryTranslator(BaseTranslator):
81
 
82
  except Exception as e:
83
  raise e
 
 
 
 
 
 
9
  """
10
  class that uses google translate to translate texts
11
  """
12
+ _languages = GOOGLE_LANGUAGES_TO_CODES
13
+ supported_languages = list(_languages.keys())
14
 
15
+ def __init__(self, source="auto", target="en", **kwargs):
16
  """
17
  @param source: source language to translate from
18
  @param target: target language to translate to
19
  """
20
  self.__base_url = BASE_URLS.get("MYMEMORY")
21
+ self._source = source if source != 'auto' else 'Lao'
22
  self._target = target
23
 
24
  self.email = kwargs.get('email', None)
 
28
  payload_key='q',
29
  langpair='{}|{}'.format(self._source, self._target))
30
 
31
+ @staticmethod
32
+ def get_supported_languages(as_dict=False):
33
+ return MyMemoryTranslator.supported_languages if not as_dict else MyMemoryTranslator._languages
34
+
35
  def translate(self, text, **kwargs):
36
  """
37
  main function that uses google translate to translate a text
 
50
  response = requests.get(self.__base_url,
51
  params=self._url_params,
52
  headers=self.headers)
53
+
54
  data = response.json()
55
  if not data:
56
  raise Exception("Translation was not found in response!")
 
87
 
88
  except Exception as e:
89
  raise e
90
+
91
+
92
+ if __name__ == '__main__':
93
+ res = MyMemoryTranslator(source="auto", target="fr").translate("you are cute")
94
+ print(res)
deep_translator/pons.py CHANGED
@@ -11,9 +11,10 @@ class PonsTranslator(BaseTranslator):
11
  """
12
  class that uses PONS translator to translate words
13
  """
14
- supported_languages = list(PONS_LANGUAGES_TO_CODES.keys())
 
15
 
16
- def __init__(self, source="french", target="english"):
17
  """
18
  @param source: source language to translate from
19
  @param target: target language to translate to
@@ -31,6 +32,10 @@ class PonsTranslator(BaseTranslator):
31
  element_query={"class": "target"}
32
  )
33
 
 
 
 
 
34
  def _map_language_to_code(self, *languages, **kwargs):
35
  """
36
 
 
11
  """
12
  class that uses PONS translator to translate words
13
  """
14
+ _languages = PONS_LANGUAGES_TO_CODES
15
+ supported_languages = list(_languages.keys())
16
 
17
+ def __init__(self, source, target="english"):
18
  """
19
  @param source: source language to translate from
20
  @param target: target language to translate to
 
32
  element_query={"class": "target"}
33
  )
34
 
35
+ @staticmethod
36
+ def get_supported_languages(as_dict=False):
37
+ return PonsTranslator.supported_languages if not as_dict else PonsTranslator._languages
38
+
39
  def _map_language_to_code(self, *languages, **kwargs):
40
  """
41