-
Notifications
You must be signed in to change notification settings - Fork 1
/
Translator.py
25 lines (22 loc) · 1010 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
import requests
import json
class Translator:
def __init__(self, client_id, client_secret):
self.client_id = client_id
self.client_secret = client_secret
self.api_url = 'https://openapi.naver.com/v1/papago/n2mt'
self.headers = {
'Content-Type': 'application/json',
'X-Naver-Client-Id': client_id,
'X-Naver-Client-Secret': client_secret
}
def translate(self, text, source_lang='ko', target_lang='en'):
try:
data = {'source': source_lang, 'target': target_lang, 'text': text}
response = requests.post(self.api_url, json.dumps(data), headers=self.headers)
response.raise_for_status() # 오류 발생한 경우 예외 발생
translated_text = response.json()['message']['result']['translatedText']
return translated_text
except requests.exceptions.RequestException as e:
print(f"Translator Error: {e}")
return None