LibreTranslate Python Bindings
libretranslatepy provides Python bindings for the LibreTranslate API, a free and open-source machine translation API. It allows Python applications to interact with a LibreTranslate server for translation, language detection, and language listing. The library is currently at version 2.1.4, released in April 2024, and is actively maintained as a client for the continuously developed LibreTranslate ecosystem.
Warnings
- gotcha Public LibreTranslate instances (e.g., `libretranslate.com`) often require an API key for translation requests, which may not be immediately obvious. Without a valid key, requests can result in HTTP errors like `403 Forbidden` or `400 Bad Request`.
- gotcha `libretranslatepy` is a client library only. It does not include a LibreTranslate server. To perform translations, you must have access to a running LibreTranslate server instance, either self-hosted or a public one. Attempting to use the library without a reachable server will result in connection errors.
- gotcha Translation quality can vary, and the underlying LibreTranslate models might occasionally return the original input, a partial translation, or corrupted text if specific language models are missing, faulty, or if the input contains complex structures (e.g., specific HTML, long numerical sequences, or placeholders). This can happen without explicit error messages from the API.
- deprecated While `libretranslatepy`'s `setup.py` declares compatibility with Python `>=2.6`, Python 2.x is officially End-of-Life and is not supported by the core LibreTranslate server project (which requires Python `>=3.8`). Using `libretranslatepy` with Python 2.x is highly discouraged and likely to lead to compatibility issues or unsupported behavior with modern LibreTranslate servers.
Install
-
pip install libretranslatepy
Imports
- LibreTranslateAPI
from libretranslatepy import LibreTranslateAPI
Quickstart
import os
from libretranslatepy import LibreTranslateAPI
# Configure LibreTranslate server URL and optional API Key
# For public instances like libretranslate.com, an API key is often required.
# For a self-hosted instance, an API key might be optional depending on server configuration.
LIBRETRANSLATE_URL = os.environ.get('LIBRETRANSLATE_URL', 'https://libretranslate.com/')
LIBRETRANSLATE_API_KEY = os.environ.get('LIBRETRANSLATE_API_KEY', None)
# Initialize the API client
lt = LibreTranslateAPI(LIBRETRANSLATE_URL, api_key=LIBRETRANSLATE_API_KEY)
# Translate text from English to Spanish
text_to_translate = "Hello, how are you?"
source_lang = "en"
target_lang = "es"
try:
translated_text = lt.translate(text_to_translate, source_lang, target_lang)
print(f"Original ({source_lang}): {text_to_translate}")
print(f"Translated ({target_lang}): {translated_text}")
# Detect language
detection_result = lt.detect("Hola mundo")
print(f"Detected language for 'Hola mundo': {detection_result}")
# List available languages
languages = lt.languages()
# print(f"Available languages: {languages}") # Uncomment to see all
print(f"Number of available languages: {len(languages)}")
except Exception as e:
print(f"An error occurred: {e}")
if 'HTTP Error 403: Forbidden' in str(e) or 'HTTP Error 400: Bad Request' in str(e):
print("\nHint: Public LibreTranslate instances often require an API key.")
print("Set the LIBRETRANSLATE_API_KEY environment variable or ensure your server doesn't require one.")