Synchronous and asynchronous Python client for the LibreTranslate API, built on httpx.
pip install libretranslate-aiofrom libretranslate_aio import LibreTranslate
with LibreTranslate() as client:
result = client.translate("Hello world!", source="en", target="pt")
print(result.translated_text) # Olá mundo!import asyncio
from libretranslate_aio import AsyncLibreTranslate
async def main():
async with AsyncLibreTranslate() as client:
result = await client.translate("Hello world!", source="en", target="pt")
print(result.translated_text)
asyncio.run(main())LibreTranslate(
base_url="https://libretranslate.com", # or your self-hosted instance
api_key="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", # optional
timeout=30.0,
)Environment variables (used by the CLI):
LIBRETRANSLATE_URL— API base URLLIBRETRANSLATE_API_KEY— API key
| Method | Description |
|---|---|
translate(q, source, target, format="text", alternatives=0) |
Translate text (str or list[str]) |
translate_file(file, source, target) |
Translate a file |
detect(q) |
Detect the text language |
languages() |
List supported languages |
suggest(q, s, source, target) |
Submit a translation suggestion |
frontend_settings() |
Frontend settings |
health() |
Health check (True if ok) |
# Translate
libretranslate translate "Hello world!" --source en --target pt
# Detect language
libretranslate detect "Bonjour le monde"
# List languages
libretranslate languages
# Translate a file
libretranslate translate-file document.txt --source en --target es -o output.txt
# Global options
libretranslate --url https://my-instance.local --api-key KEY translate "Hi" -t ptuv sync --all-extras
uv run ruff check .
uv run pytest
uv build- Create the repository on GitHub and push.
- On PyPI, configure Trusted Publishing pointing to the
publish.ymlworkflow. - Create a
vX.Y.Zrelease/tag — GitHub Actions publishes automatically.
MIT