Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/argosopentech/libretranslate-py
Python bindings to connect to a LibreTranslate API
https://github.com/argosopentech/libretranslate-py
Last synced: 14 days ago
JSON representation
Python bindings to connect to a LibreTranslate API
- Host: GitHub
- URL: https://github.com/argosopentech/libretranslate-py
- Owner: argosopentech
- License: mit
- Created: 2021-05-30T21:03:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-04T23:00:05.000Z (2 months ago)
- Last Synced: 2024-10-16T13:18:14.721Z (27 days ago)
- Language: Python
- Homepage: https://pypi.org/project/libretranslatepy/
- Size: 42 KB
- Stars: 87
- Watchers: 4
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LibreTranslate-py
Python bindings to connect to a [LibreTranslate API](https://github.com/LibreTranslate/LibreTranslate)
## Install
```
pip install libretranslatepy
```## Example usage
```python
from libretranslatepy import LibreTranslateAPIlt = LibreTranslateAPI("https://translate.terraprint.co/")
print(lt.translate("LibreTranslate is awesome!", "en", "es"))
# LibreTranslate es impresionante!print(lt.detect("Hello World"))
# [{"confidence": 0.6, "language": "en"}]print(lt.languages())
# [{"code":"en", "name":"English"}]
```## [LibreTranslate Mirrors](https://github.com/LibreTranslate/LibreTranslate#mirrors)
## Source
```python
import json
import sys
from urllib import request, parseclass LibreTranslateAPI:
"""Connect to the LibreTranslate API""""""Example usage:
from libretranslatepy import LibreTranslateAPIlt = LibreTranslateAPI("https://translate.terraprint.co/")
print(lt.translate("LibreTranslate is awesome!", "en", "es"))
# LibreTranslate es impresionante!print(lt.detect("Hello World"))
# [{"confidence": 0.6, "language": "en"}]
print(lt.languages())
# [{"code":"en", "name":"English"}]
"""DEFAULT_URL = "https://translate.terraprint.co/"
def __init__(self, url=None, api_key=None):
"""Create a LibreTranslate API connection.Args:
url (str): The url of the LibreTranslate endpoint.
api_key (str): The API key.
"""
self.url = LibreTranslateAPI.DEFAULT_URL if url is None else url
self.api_key = api_key# Add trailing slash
assert len(self.url) > 0
if self.url[-1] != "/":
self.url += "/"def translate(self, q, source="en", target="es"):
"""Translate stringArgs:
q (str): The text to translate
source (str): The source language code (ISO 639)
target (str): The target language code (ISO 639)Returns:
str: The translated text
"""
url = self.url + "translate"
params = {"q": q, "source": source, "target": target}
if self.api_key is not None:
params["api_key"] = self.api_key
url_params = parse.urlencode(params)
req = request.Request(url, data=url_params.encode())
response = request.urlopen(req)
response_str = response.read().decode()
return json.loads(response_str)["translatedText"]def detect(self, q):
"""Detect the language of a single text.Args:
q (str): Text to detectReturns:
The detected languages ex: [{"confidence": 0.6, "language": "en"}]
"""
url = self.url + "detect"
params = {"q": q}
if self.api_key is not None:
params["api_key"] = self.api_key
url_params = parse.urlencode(params)
req = request.Request(url, data=url_params.encode())
response = request.urlopen(req)
response_str = response.read().decode()
return json.loads(response_str)def languages(self):
"""Retrieve list of supported languages.Returns:
A list of available languages ex: [{"code":"en", "name":"English"}]
"""
url = self.url + "languages"
params = dict()
if self.api_key is not None:
params["api_key"] = self.api_key
url_params = parse.urlencode(params)
req = request.Request(url, data=url_params.encode(), method="GET")
response = request.urlopen(req)
response_str = response.read().decode()
return json.loads(response_str)```
## License
Licensed under either the MIT License or Public DomainDeveloped by P.J. Finlay