Commit b5d46091 authored by Jakob Lerch's avatar Jakob Lerch
Browse files

add abstract class for translation

parent 7f9d6a71
from abc import ABC, abstractmethod
import requests
import json
class Translation(ABC):
@abstractmethod
def translate(self, src_string: str, src_lang: str, dst_lang: str) -> str:
pass
class DeepL(Translation):
def __init__(self, url: str, api_key: str):
raise NotImplementedError()
def translate(self, src_string: str, src_lang: str, dst_lang: str) -> str:
raise NotImplementedError()
class LibreTranslate(Translation):
def __init__(self, url: str, api_key: str):
self.url = url
self.api_key = api_key
def translate(self, src_string: str, src_lang: str, dst_lang: str) -> str:
r = requests.post(
self.url + "/translate",
data={
"q": src_string if src_string != "" else " ",
"source": src_lang,
"target": dst_lang,
"format": "text"
})
return json.loads(r.text)["translatedText"]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment