-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.py
31 lines (25 loc) · 929 Bytes
/
translator.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
import requests
class Translator:
def __init__(self, api_key):
self.api_key = api_key
def translate(self, text, target_lang, source_lang):
result = self.translate_with_deepl(text, target_lang, source_lang)
return result
def translate_with_deepl(self,text, target_lang, source_lang):
translated_text = ""
try:
# APIから翻訳情報を取得
result = requests.get(
# 無料版のURL
"https://api-free.deepl.com/v2/translate",
params={
"auth_key": self.api_key,
"target_lang": target_lang,
"source_lang": source_lang,
"text": text,
},
)
translated_text = result.json()["translations"][0]["text"]
except:
return "deepL api error"
return translated_text