kuspia
commited on
Update google_trans.py
Browse files
deep_translator/google_trans.py
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
google translator API
|
3 |
"""
|
4 |
|
5 |
-
from .constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
|
6 |
from .exceptions import TooManyRequests, LanguageNotSupportedException, TranslationNotFound, NotValidPayload, RequestError
|
7 |
from .parent import BaseTranslator
|
8 |
from bs4 import BeautifulSoup
|
@@ -27,8 +27,19 @@ class GoogleTranslator(BaseTranslator):
|
|
27 |
self.__base_url = BASE_URLS.get("GOOGLE_TRANSLATE")
|
28 |
self.proxies = proxies
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
super(GoogleTranslator, self).__init__(base_url=self.__base_url,
|
34 |
source=self._source,
|
@@ -51,6 +62,17 @@ class GoogleTranslator(BaseTranslator):
|
|
51 |
"""
|
52 |
return GoogleTranslator.supported_languages if not as_dict else GoogleTranslator._languages
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
def _map_language_to_code(self, *languages):
|
55 |
"""
|
56 |
map language to its corresponding code (abbreviation) if the language was passed by its full name by the user
|
@@ -62,6 +84,8 @@ class GoogleTranslator(BaseTranslator):
|
|
62 |
yield language
|
63 |
elif language in self._languages.keys():
|
64 |
yield self._languages[language]
|
|
|
|
|
65 |
else:
|
66 |
raise LanguageNotSupportedException(language)
|
67 |
|
@@ -74,7 +98,8 @@ class GoogleTranslator(BaseTranslator):
|
|
74 |
for lang in languages:
|
75 |
if lang != 'auto' and lang not in self._languages.keys():
|
76 |
if lang != 'auto' and lang not in self._languages.values():
|
77 |
-
|
|
|
78 |
return True
|
79 |
|
80 |
def translate(self, text, **kwargs):
|
@@ -181,8 +206,6 @@ class GoogleTranslator(BaseTranslator):
|
|
181 |
|
182 |
return arr
|
183 |
|
184 |
-
|
185 |
-
|
186 |
if __name__ == '__main__':
|
187 |
translator = GoogleTranslator(source='ru', target='uk')
|
188 |
t = translator.translate("Я разработчик") # => "I am a developer"
|
|
|
2 |
google translator API
|
3 |
"""
|
4 |
|
5 |
+
from .constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES, GOOGLE_LANGUAGES_SECONDARY_NAMES
|
6 |
from .exceptions import TooManyRequests, LanguageNotSupportedException, TranslationNotFound, NotValidPayload, RequestError
|
7 |
from .parent import BaseTranslator
|
8 |
from bs4 import BeautifulSoup
|
|
|
27 |
self.__base_url = BASE_URLS.get("GOOGLE_TRANSLATE")
|
28 |
self.proxies = proxies
|
29 |
|
30 |
+
# code snipppet that converts the language into lower-case and skip lower-case conversion for abbreviations
|
31 |
+
# since abbreviations like zh-CN if converted to lower-case will result into error
|
32 |
+
#######################################
|
33 |
+
source_map = source
|
34 |
+
target_map = target
|
35 |
+
if not( source in self._languages.values()):
|
36 |
+
source_map=source.lower()
|
37 |
+
if not( target in self._languages.values()):
|
38 |
+
target_map=target.lower()
|
39 |
+
#######################################
|
40 |
+
|
41 |
+
if self.is_language_supported(source_map, target_map):
|
42 |
+
self._source, self._target = self._map_language_to_code(source_map, target_map)
|
43 |
|
44 |
super(GoogleTranslator, self).__init__(base_url=self.__base_url,
|
45 |
source=self._source,
|
|
|
62 |
"""
|
63 |
return GoogleTranslator.supported_languages if not as_dict else GoogleTranslator._languages
|
64 |
|
65 |
+
def is_secondary(self, lang):
|
66 |
+
"""
|
67 |
+
Function to check if lang is a secondary name of any primary language
|
68 |
+
@param lang: language name
|
69 |
+
@return: primary name of a language if found otherwise 0
|
70 |
+
"""
|
71 |
+
for primary_name, secondary_names in GOOGLE_LANGUAGES_SECONDARY_NAMES.items():
|
72 |
+
if (lang in secondary_names):
|
73 |
+
return primary_name
|
74 |
+
return 0
|
75 |
+
|
76 |
def _map_language_to_code(self, *languages):
|
77 |
"""
|
78 |
map language to its corresponding code (abbreviation) if the language was passed by its full name by the user
|
|
|
84 |
yield language
|
85 |
elif language in self._languages.keys():
|
86 |
yield self._languages[language]
|
87 |
+
elif (self.is_secondary(language)!=0):
|
88 |
+
yield self._languages[self.is_secondary(language)]
|
89 |
else:
|
90 |
raise LanguageNotSupportedException(language)
|
91 |
|
|
|
98 |
for lang in languages:
|
99 |
if lang != 'auto' and lang not in self._languages.keys():
|
100 |
if lang != 'auto' and lang not in self._languages.values():
|
101 |
+
if (self.is_secondary(lang)==0):
|
102 |
+
raise LanguageNotSupportedException(lang)
|
103 |
return True
|
104 |
|
105 |
def translate(self, text, **kwargs):
|
|
|
206 |
|
207 |
return arr
|
208 |
|
|
|
|
|
209 |
if __name__ == '__main__':
|
210 |
translator = GoogleTranslator(source='ru', target='uk')
|
211 |
t = translator.translate("Я разработчик") # => "I am a developer"
|