diff --git a/README.rst b/README.rst index 01de65b..dfd448f 100644 --- a/README.rst +++ b/README.rst @@ -7,7 +7,7 @@ Translate Tool in Python Translate is a simple but powerful translation tool written in python with with support for multiple translation providers. By now we offer integration with Microsoft Translation API, -Translated MyMemory API and DeepL's free and pro APIs +Translated MyMemory API, LibreTranslate, and DeepL's free and pro APIs Why Should I Use This? diff --git a/docs/source/providers.rst b/docs/source/providers.rst index 3b7d07c..82ebd1c 100644 --- a/docs/source/providers.rst +++ b/docs/source/providers.rst @@ -40,3 +40,22 @@ for further information abount the provider: https://mymemory.translated.net/doc/spec.php http://mymemory.translated.net/doc/usagelimits.php + +LibreTranslate +-------- + +Free and open source translation provider + +``LibreTranslate`` (located at ``translate.providers``) receives the following options: + +to_lang, from_lang='en', secret_access_key=None, base_url="https://translate.astian.org/" + + * ``to_lang``: language you want to translate + * ``from_lang``: Language of the text being translated (optional): as default ``autodetect`` + * ``secret_access_key``: LibreTranslate API key + * ``base_url``: LibreTranslate instance url + +for further information abount the provider: + + https://libretranslate.com + http://github.com/LibreTranslate/LibreTranslate diff --git a/requirements.txt b/requirements.txt index 1070230..d8006cf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ click lxml requests +libretranslatepy==2.1.1 diff --git a/translate/providers/libre.py b/translate/providers/libre.py index 236fec6..a318f74 100644 --- a/translate/providers/libre.py +++ b/translate/providers/libre.py @@ -1,47 +1,35 @@ #!/usr/bin/env python # encoding: utf-8 import requests +from libretranslatepy import LibreTranslateAPI import json from .base import BaseProvider from ..exceptions import TranslationError - class LibreProvider(BaseProvider): """ - @LibreProvider: This is a integration with LibreTranslate Translator API. + @LibreProvider: This is a integration with LibreTranslate translation API. Website: https://libretranslate.com/ Documentation: https://libretranslate.com/docs/ - Github: https://github.com/uav4geo/LibreTranslate + Github: https://github.com/LibreTranslate/LibreTranslate """ name = "Libre" - def _make_request(self, text): - params = { - "api_key": self.secret_access_key, - "target": self.to_lang, - "q": text, - "source": self.from_lang, - } - - response = requests.post( - self.base_url, params=params, headers=self.headers, json=[{}] - ) - return response.json() + def __init__(self, to_lang, from_lang='en', secret_access_key=None, region=None, base_url=None, **kwargs): + super().__init__(to_lang) + self.base_url = base_url + self.api = LibreTranslateAPI(base_url, secret_access_key) def get_translation(self, text): - if self.base_url == "": - raise TranslationError( - """ - Please provide a base_url in constructor - for example, - translator = Translator(to_lang = "en" , base_url = "http://localhost:5000/") - """ - ) - data = self._make_request(text) + if self.from_lang == 'autodetect': + from_lang = self.api.detect(text)[0]['language'] + else: + from_lang = self.from_lang - if "error" in data: - raise TranslationError(data["error"]) + try: + return self.api.translate(text, from_lang, self.to_lang) + except Exception as e: + raise TranslationError(e) - return data["translatedText"]