2022-01-24 04:07:52 +00:00
|
|
|
"""
|
|
|
|
LibreTranslate API
|
|
|
|
"""
|
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
from typing import List, Optional
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
import requests
|
2022-05-01 12:00:20 +00:00
|
|
|
|
|
|
|
from deep_translator.base import BaseTranslator
|
|
|
|
from deep_translator.constants import BASE_URLS, LIBRE_LANGUAGES_TO_CODES
|
|
|
|
from deep_translator.exceptions import (
|
|
|
|
AuthorizationException,
|
|
|
|
ServerException,
|
|
|
|
TranslationNotFound,
|
|
|
|
)
|
|
|
|
from deep_translator.validate import is_empty, is_input_valid
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LibreTranslator(BaseTranslator):
|
|
|
|
"""
|
|
|
|
class that wraps functions, which use libre translator under the hood to translate text(s)
|
|
|
|
"""
|
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
api_key: Optional[str] = None,
|
|
|
|
source: str = "en",
|
|
|
|
target: str = "es",
|
|
|
|
use_free_api: bool = True,
|
2022-11-07 18:06:49 +00:00
|
|
|
custom_url: Optional[str] = None,
|
2022-05-01 12:00:20 +00:00
|
|
|
**kwargs
|
|
|
|
):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
@param api_key: your api key
|
2022-01-24 04:07:52 +00:00
|
|
|
@param source: source language to translate from
|
2022-05-01 12:00:20 +00:00
|
|
|
List of LibreTranslate endpoint can be found at : https://github.com/LibreTranslate/LibreTranslate#mirrors
|
2022-01-24 04:07:52 +00:00
|
|
|
Some require an API key
|
|
|
|
@param target: target language to translate to
|
2022-05-01 12:00:20 +00:00
|
|
|
@param use_free_api: set True if you want to use the free api. This means a url that does not require and api key would be used
|
|
|
|
@param custom_url: you can use a custom endpoint
|
2022-01-24 04:07:52 +00:00
|
|
|
"""
|
|
|
|
self.api_key = api_key
|
2022-11-07 18:06:49 +00:00
|
|
|
url = (
|
|
|
|
BASE_URLS.get("LIBRE") if not use_free_api else BASE_URLS.get("LIBRE_FREE")
|
|
|
|
)
|
2022-05-01 12:00:20 +00:00
|
|
|
super().__init__(
|
|
|
|
base_url=url if not custom_url else custom_url,
|
|
|
|
source=source,
|
|
|
|
target=target,
|
|
|
|
languages=LIBRE_LANGUAGES_TO_CODES,
|
|
|
|
)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def translate(self, text: str, **kwargs) -> str:
|
2022-01-24 04:07:52 +00:00
|
|
|
"""
|
|
|
|
function that uses microsoft translate to translate a text
|
|
|
|
@param text: desired text to translate
|
|
|
|
@return: str: translated text
|
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
if is_input_valid(text):
|
|
|
|
if self._same_source_target() or is_empty(text):
|
|
|
|
return text
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
translate_endpoint = "translate"
|
|
|
|
params = {
|
|
|
|
"q": text,
|
|
|
|
"source": self._source,
|
|
|
|
"target": self._target,
|
|
|
|
"format": "text",
|
|
|
|
}
|
|
|
|
# Add API Key if required
|
|
|
|
if self.api_key:
|
|
|
|
params["api_key"] = self.api_key
|
|
|
|
# Do the request and check the connection.
|
|
|
|
try:
|
|
|
|
response = requests.post(
|
|
|
|
self._base_url + translate_endpoint, params=params
|
|
|
|
)
|
|
|
|
except ConnectionError:
|
|
|
|
raise ServerException(503)
|
|
|
|
# If the answer is not success, raise server exception.
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
if response.status_code == 403:
|
|
|
|
raise AuthorizationException(self.api_key)
|
|
|
|
elif response.status_code != 200:
|
|
|
|
raise ServerException(response.status_code)
|
|
|
|
# Get the response and check is not empty.
|
|
|
|
res = response.json()
|
|
|
|
if not res:
|
|
|
|
raise TranslationNotFound(text)
|
|
|
|
# Process and return the response.
|
|
|
|
return res["translatedText"]
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def translate_file(self, path: str, **kwargs) -> str:
|
2022-01-24 04:07:52 +00:00
|
|
|
"""
|
|
|
|
translate directly from file
|
|
|
|
@param path: path to the target file
|
|
|
|
@type path: str
|
|
|
|
@param kwargs: additional args
|
|
|
|
@return: str
|
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
return self._translate_file(path, **kwargs)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
2022-01-24 04:07:52 +00:00
|
|
|
"""
|
|
|
|
translate a list of texts
|
|
|
|
@param batch: list of texts you want to translate
|
|
|
|
@return: list of translations
|
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
return self._translate_batch(batch, **kwargs)
|
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
if __name__ == "__main__":
|
2022-05-01 12:00:20 +00:00
|
|
|
l = LibreTranslator(source="en", target="de")
|
|
|
|
res = l.translate("good")
|
|
|
|
print("res: ", res)
|