-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
33 lines (28 loc) · 1.21 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import requests
import json
def get_translate(text: str, targetLang: str, sourceLang: str="auto") -> str:
"""
Translate text by requests
:param text: source text
:param targetLang: target language
:param sourceLang: source language
:return: translation text
"""
if not text:
return text
google_url = f"https://translate.googleapis.com/translate_a/single?client=gtx&sl={sourceLang}&tl={targetLang}&dt=t&q={text}"
try:
response = requests.get(google_url)
response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
translation_data = json.loads(response.text)
return "".join(item[0] for item in translation_data[0])
except requests.RequestException as e:
print(f"Error... during the request to translate text")
except (IndexError, TypeError):
print("Error... Could not retrieve translation from response")
except json.JSONDecodeError:
print("Error... decoding the JSON response during translation")
except Exception as err:
print(f"An unexpected error during translation occurred: {err}")
return text # Return original text if translation fails