-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from PJ-Finlay/master
Improved LibreTranslate support
- Loading branch information
Showing
4 changed files
with
36 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
click | ||
lxml | ||
requests | ||
libretranslatepy==2.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |