risdom / translate
yosuke-i's picture
Create translate
6415905 verified
raw
history blame
664 Bytes
import requests
import json
def translate_text(text, target_lang='ja', source_lang='en'):
api_key = "AIzaSyAEzK5_n6zKTimD9yoXS-C8O0xN_4LaVBQ"
url = f"https://translation.googleapis.com/language/translate/v2?key={api_key}"
payload = {
'q': text,
'source': source_lang,
'target': target_lang,
'format': 'text'
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
return json.loads(response.text)['data']['translations'][0]['translatedText']
else:
return "Error: " + response.text