nidhal baccouri
commited on
Commit
·
219958c
1
Parent(s):
1641572
fixed microsoft supported languages bug
Browse files- deep_translator/microsoft.py +15 -12
deep_translator/microsoft.py
CHANGED
@@ -32,8 +32,6 @@ class MicrosoftTranslator(BaseTranslator):
|
|
32 |
@param region: your Microsoft Location
|
33 |
"""
|
34 |
|
35 |
-
MICROSOFT_CODES_TO_LANGUAGES = self._get_supported_languages()
|
36 |
-
|
37 |
if not api_key:
|
38 |
raise ServerException(401)
|
39 |
|
@@ -51,17 +49,21 @@ class MicrosoftTranslator(BaseTranslator):
|
|
51 |
base_url=BASE_URLS.get("MICROSOFT_TRANSLATE"),
|
52 |
source=source,
|
53 |
target=target,
|
54 |
-
languages=
|
55 |
**kwargs,
|
56 |
)
|
57 |
|
|
|
|
|
|
|
58 |
def _get_supported_languages(self):
|
59 |
|
60 |
-
microsoft_languages_api_url = "https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope
|
|
|
61 |
microsoft_languages_response = requests.get(microsoft_languages_api_url)
|
62 |
translation_dict = microsoft_languages_response.json()["translation"]
|
63 |
|
64 |
-
return {translation_dict[k]["name"].lower(): k for k in translation_dict.keys()}
|
65 |
|
66 |
def translate(self, text: str, **kwargs) -> str:
|
67 |
"""
|
@@ -71,14 +73,14 @@ class MicrosoftTranslator(BaseTranslator):
|
|
71 |
"""
|
72 |
# a body must be a list of dicts to process multiple texts;
|
73 |
# I have not added multiple text processing here since it is covered by the translate_batch method
|
74 |
-
|
75 |
if is_input_valid(text):
|
76 |
self._url_params["from"] = self._source
|
77 |
self._url_params["to"] = self._target
|
78 |
|
79 |
valid_microsoft_json = [{"text": text}]
|
80 |
try:
|
81 |
-
|
82 |
self._base_url,
|
83 |
params=self._url_params,
|
84 |
headers=self.headers,
|
@@ -90,13 +92,14 @@ class MicrosoftTranslator(BaseTranslator):
|
|
90 |
logging.warning(f"Returned error: {exc_type.__name__}")
|
91 |
|
92 |
# Where Microsoft API responds with an api error, it returns a dict in response.json()
|
93 |
-
if type(
|
94 |
-
error_message =
|
95 |
raise MicrosoftAPIerror(error_message)
|
96 |
-
# Where it responds with a translation, its response.json() is a list
|
97 |
-
|
|
|
98 |
all_translations = [
|
99 |
-
i["text"] for i in
|
100 |
]
|
101 |
return "\n".join(all_translations)
|
102 |
|
|
|
32 |
@param region: your Microsoft Location
|
33 |
"""
|
34 |
|
|
|
|
|
35 |
if not api_key:
|
36 |
raise ServerException(401)
|
37 |
|
|
|
49 |
base_url=BASE_URLS.get("MICROSOFT_TRANSLATE"),
|
50 |
source=source,
|
51 |
target=target,
|
52 |
+
languages=self._get_supported_languages(),
|
53 |
**kwargs,
|
54 |
)
|
55 |
|
56 |
+
# this function get the actual supported languages of the msft translator and store them in a dict, where
|
57 |
+
# the keys are the abbreviations and the values are the languages
|
58 |
+
# a common variable used in the other translators would be: MICROSOFT_CODES_TO_LANGUAGES
|
59 |
def _get_supported_languages(self):
|
60 |
|
61 |
+
microsoft_languages_api_url = "https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope" \
|
62 |
+
"=translation "
|
63 |
microsoft_languages_response = requests.get(microsoft_languages_api_url)
|
64 |
translation_dict = microsoft_languages_response.json()["translation"]
|
65 |
|
66 |
+
return {translation_dict[k]["name"].lower(): k.lower() for k in translation_dict.keys()}
|
67 |
|
68 |
def translate(self, text: str, **kwargs) -> str:
|
69 |
"""
|
|
|
73 |
"""
|
74 |
# a body must be a list of dicts to process multiple texts;
|
75 |
# I have not added multiple text processing here since it is covered by the translate_batch method
|
76 |
+
response = None
|
77 |
if is_input_valid(text):
|
78 |
self._url_params["from"] = self._source
|
79 |
self._url_params["to"] = self._target
|
80 |
|
81 |
valid_microsoft_json = [{"text": text}]
|
82 |
try:
|
83 |
+
response = requests.post(
|
84 |
self._base_url,
|
85 |
params=self._url_params,
|
86 |
headers=self.headers,
|
|
|
92 |
logging.warning(f"Returned error: {exc_type.__name__}")
|
93 |
|
94 |
# Where Microsoft API responds with an api error, it returns a dict in response.json()
|
95 |
+
if type(response.json()) is dict:
|
96 |
+
error_message = response.json()["error"]
|
97 |
raise MicrosoftAPIerror(error_message)
|
98 |
+
# Where it responds with a translation, its response.json() is a list
|
99 |
+
# e.g. [{'translations': [{'text':'Hello world!', 'to': 'en'}]}]
|
100 |
+
elif type(response.json()) is list:
|
101 |
all_translations = [
|
102 |
+
i["text"] for i in response.json()[0]["translations"]
|
103 |
]
|
104 |
return "\n".join(all_translations)
|
105 |
|