nidhal baccouri
commited on
Commit
·
939c119
1
Parent(s):
a98dd06
fixed bug in google trans
Browse files- deep_translator/google_trans.py +17 -6
- deep_translator/parent.py +1 -1
deep_translator/google_trans.py
CHANGED
@@ -91,6 +91,7 @@ class GoogleTranslator(BaseTranslator):
|
|
91 |
response = requests.get(self.__base_url,
|
92 |
params=self._url_params, headers ={'User-agent': 'your bot 0.1'})
|
93 |
|
|
|
94 |
if response.status_code == 429:
|
95 |
raise TooManyRequests()
|
96 |
|
@@ -105,10 +106,13 @@ class GoogleTranslator(BaseTranslator):
|
|
105 |
element = soup.find(self._element_tag, self._alt_element_query)
|
106 |
if not element:
|
107 |
raise TranslationNotFound(text)
|
108 |
-
if element.get_text(strip=True) == text.strip()
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
112 |
else:
|
113 |
return element.get_text(strip=True)
|
114 |
|
@@ -173,5 +177,12 @@ class GoogleTranslator(BaseTranslator):
|
|
173 |
|
174 |
if __name__ == '__main__':
|
175 |
|
176 |
-
txt =GoogleTranslator(
|
177 |
-
print("text: ", txt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
response = requests.get(self.__base_url,
|
92 |
params=self._url_params, headers ={'User-agent': 'your bot 0.1'})
|
93 |
|
94 |
+
# print(response.url)
|
95 |
if response.status_code == 429:
|
96 |
raise TooManyRequests()
|
97 |
|
|
|
106 |
element = soup.find(self._element_tag, self._alt_element_query)
|
107 |
if not element:
|
108 |
raise TranslationNotFound(text)
|
109 |
+
if element.get_text(strip=True) == text.strip():
|
110 |
+
to_translate_alpha = ''.join(ch for ch in text.strip() if ch.isalnum())
|
111 |
+
translated_alpha = ''.join(ch for ch in element.get_text(strip=True) if ch.isalnum())
|
112 |
+
if to_translate_alpha and translated_alpha and to_translate_alpha == translated_alpha:
|
113 |
+
self._url_params["tl"] = self._target
|
114 |
+
del self._url_params["hl"]
|
115 |
+
return self.translate(text)
|
116 |
else:
|
117 |
return element.get_text(strip=True)
|
118 |
|
|
|
177 |
|
178 |
if __name__ == '__main__':
|
179 |
|
180 |
+
# txt =GoogleTranslator(target='irish').translate('how are you')
|
181 |
+
# print("text: ", txt)
|
182 |
+
translator = GoogleTranslator(target="irish")
|
183 |
+
|
184 |
+
text_to_translate = "Hello, how are you!?"
|
185 |
+
|
186 |
+
translated_text = translator.translate(text_to_translate)
|
187 |
+
|
188 |
+
print(translated_text)
|
deep_translator/parent.py
CHANGED
@@ -40,7 +40,7 @@ class BaseTranslator(ABC):
|
|
40 |
@return: bool
|
41 |
"""
|
42 |
|
43 |
-
if not payload or not isinstance(payload, str):
|
44 |
raise NotValidPayload(payload)
|
45 |
if not BaseTranslator.__check_length(payload, min_chars, max_chars):
|
46 |
raise NotValidLength(payload, min_chars, max_chars)
|
|
|
40 |
@return: bool
|
41 |
"""
|
42 |
|
43 |
+
if not payload or not isinstance(payload, str) or not payload.strip():
|
44 |
raise NotValidPayload(payload)
|
45 |
if not BaseTranslator.__check_length(payload, min_chars, max_chars):
|
46 |
raise NotValidLength(payload, min_chars, max_chars)
|