Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved LibreTranslate support #76

Merged
merged 2 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
19 changes: 19 additions & 0 deletions docs/source/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
click
lxml
requests
libretranslatepy==2.1.1
42 changes: 15 additions & 27 deletions translate/providers/libre.py
Original file line number Diff line number Diff line change
@@ -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"]