

LibreTranslate
6.7k 635What is LibreTranslate ?
LibreTranslate is a Free and Open Source Machine Translation API, entirely self-hosted. Unlike other APIs, it doesn’t rely on proprietary providers such as Google or Azure to perform translations. Instead, its translation engine is powered by the open source Argos Translate library.
LibreTranslate Demo
Try it online! | API Docs | Community Forum
API Examples
Simple
Request:
const res = await fetch("https://libretranslate.com/translate", { method: "POST", body: JSON.stringify({ q: "Hello!", source: "en", target: "es" }), headers: { "Content-Type": "application/json" }});
console.log(await res.json());
Response:
{ "translatedText": "¡Hola!"}
Auto Detect Language
Request:
const res = await fetch("https://libretranslate.com/translate", { method: "POST", body: JSON.stringify({ q: "Ciao!", source: "auto", target: "en" }), headers: { "Content-Type": "application/json" }});
console.log(await res.json());
Response:
{ "detectedLanguage": { "confidence": 83, "language": "it" }, "translatedText": "Bye!"}
HTML (beta)
Request:
const res = await fetch("https://libretranslate.com/translate", { method: "POST", body: JSON.stringify({ q: '<p class="green">Hello!</p>', source: "en", target: "es", format: "html" }), headers: { "Content-Type": "application/json" }});
console.log(await res.json());
Response:
{ "translatedText": "<p class=\"green\">¡Hola!</p>"}
Install and Run
You can run your own API server with just a few lines of setup!
Make sure you have Python installed (3.8 or higher is recommended), then simply run:
pip install libretranslatelibretranslate [args]
Then open a web browser to http://localhost:5000
On Ubuntu 20.04 you can also use the install script available at https://github.com/argosopentech/LibreTranslate-init
Run with Docker
You can also run the application with docker:
Linux/macOS
./run.sh [args]
Windows
run.bat [args]
Build and Run
See CONTIRBUTING.md for information on how to build and run the project yourself.
CUDA
You can use hardware acceleration to speed up translations on a GPU machine with CUDA 11.2 and nvidia-docker installed.
Run this version with:
docker compose -f docker-compose.cuda.yml up -d --build
Arguments
Arguments passed to the process or set via environment variables are split into two kinds.
-
Settings or runtime flags used to toggle specific runmodes or disable parts of the application. These act as toggle when added or removed.
-
Configuration parameters to set various limits and configure the application. These require a parameter to be passed to function, if removed the default parameters are used.
Settings / Flags
Argument | Description | Default Setting | Env. name |
---|---|---|---|
—debug | Enable debug environment | Disabled | LT_DEBUG |
—ssl | Whether to enable SSL | Disabled | LT_SSL |
—api-keys | Enable API keys database for per-client rate limits when —req-limit is reached | Don't use API keys | LT_API_KEYS |
—require-api-key-origin | Require use of an API key for programmatic access to the API, unless the request origin matches this domain | No restrictions on domain origin | LT_REQUIRE_API_KEY_ORIGIN |
—require-api-key-secret | Require use of an API key for programmatic access to the API, unless the client also sends a secret match | No secrets required | LT_REQUIRE_API_KEY_SECRET |
—suggestions | Allow user suggestions | Disabled | LT_SUGGESTIONS |
—disable-files-translation | Disable files translation | File translation allowed | LT_DISABLE_FILES_TRANSLATION |
—disable-web-ui | Disable web ui | Web Ui enabled | LT_DISABLE_WEB_UI |
—update-models | Update language models at startup | Only on if no models found | LT_UPDATE_MODELS |
—metrics | Enable the /metrics endpoint for exporting Prometheus usage metrics | Disabled | LT_METRICS |
Configuration Parameters
Argument | Description | Default Parameter | Env. name |
---|---|---|---|
—host | Set host to bind the server to | 127.0.0.1 | LT_HOST |
—port | Set port to bind the server to | 5000 | LT_PORT |
—char-limit | Set character limit | No limit | LT_CHAR_LIMIT |
—req-limit | Set maximum number of requests per minute per client (outside of limits set by api keys) | No limit | LT_REQ_LIMIT |
—req-limit-storage | Storage URI to use for request limit data storage. See Flask Limiter | memory:// | LT_REQ_LIMIT_STORAGE |
—batch-limit | Set maximum number of texts to translate in a batch request | No limit | LT_BATCH_LIMIT |
—ga-id | Enable Google Analytics on the API client page by providing an ID | Empty (no tracking) | LT_GA_ID |
—frontend-language-source | Set frontend default language - source | auto | LT_FRONTEND_LANGUAGE_SOURCE |
—frontend-language-target | Set frontend default language - target | locale (match site’s locale) | LT_FRONTEND_LANGUAGE_TARGET |
—frontend-timeout | Set frontend translation timeout | 500 | LT_FRONTEND_TIMEOUT |
—api-keys-db-path | Use a specific path inside the container for the local database. Can be absolute or relative | db/api_keys.db | LT_API_KEYS_DB_PATH |
—api-keys-remote | Use this remote endpoint to query for valid API keys instead of using the local database | Empty (use local db instead) | LT_API_KEYS_REMOTE |
—get-api-key-link | Show a link in the UI where to direct users to get an API key | Empty (no link shown on web ui) | LT_GET_API_KEY_LINK |
—shared-storage | Shared storage URI to use for multi-process data sharing (e.g. when using gunicorn) | memory:// | LT_SHARED_STORAGE |
—load-only | Set available languages | Empty (use all from argostranslate) | LT_LOAD_ONLY |
—threads | Set number of threads | 4 | LT_THREADS |
—metrics-auth-token | Protect the /metrics endpoint by allowing only clients that have a valid Authorization Bearer token | Empty (no auth required) | LT_METRICS_AUTH_TOKEN |
—url-prefix | Add prefix to URL: example.com:5000/url-prefix/ | / | LT_URL_PREFIX |
Notes:
-
Each argument has an equivalent environment variable that can be used instead. The env. variables overwrite the default values but have lower priority than the command arguments and are particularly useful if used with Docker. The environment variable names are the upper-snake-case of the equivalent command argument’s name with a
LT
prefix. -
To configure requirement for api key to use, set
--req-limit
to0
and add the--api-keys
flag. Requests made without a proper api key will be rejected. -
Setting
--update-models
will update models regardless of whether updates are available or not.
Update
Software
If you installed with pip:
pip install -U libretranslate
If you’re using docker:
docker pull libretranslate/libretranslate
Language Models
Start the program with the --update-models
argument. For example: libretranslate --update-models
or ./run.sh --update-models
.
Alternatively you can also run the scripts/install_models.py
script.
Run with WSGI and Gunicorn
pip install gunicorngunicorn --bind 0.0.0.0:5000 'wsgi:app'
You can pass application arguments directly to Gunicorn via:
gunicorn --bind 0.0.0.0:5000 'wsgi:app(api_keys=True)'
Kubernetes Deployment
See Medium article by JM Robles and the improved k8s.yaml by @rasos.
Helm Chart
Based on @rasos work you can now install LibreTranslate on Kubernetes using Helm.
A Helm chart is now available in the helm-chart repository where you can find more details.
You can quickly install LibreTranslate on Kubernetes using Helm with the following command:
helm repo add libretranslate https://libretranslate.github.io/helm-chart/helm repo updatehelm search repo libretranslate
helm install libretranslate libretranslate/libretranslate --namespace libretranslate --create-namespace
Manage API Keys
LibreTranslate supports per-user limit quotas, e.g. you can issue API keys to users so that they can enjoy higher requests limits per minute (if you also set --req-limit
). By default all users are rate-limited based on --req-limit
, but passing an optional api_key
parameter to the REST endpoints allows a user to enjoy higher request limits. You can also specify different character limits that bypass the default --char-limit
value on a per-key basis.
To use API keys simply start LibreTranslate with the --api-keys
option. If you modified the API keys database path with the option --api-keys-db-path
, you must specify the path with the same argument flag when using the ltmanage keys
command.
Add New Keys
To issue a new API key with 120 requests per minute limits:
ltmanage keys add 120
To issue a new API key with 120 requests per minute and a maximum of 5,000 characters per request:
ltmanage keys add 120 --char-limit 5000
If you changed the API keys database path:
ltmanage keys --api-keys-db-path path/to/db/dbName.db add 120
Remove Keys
ltmanage keys remove <api-key>
View Keys
ltmanage keys
Prometheus Metrics
LibreTranslate has Prometheus exporter capabilities when you pass the --metrics
argument at startup (disabled by default). When metrics are enabled, a /metrics
endpoint is mounted on the instance:
# HELP libretranslate_http_requests_in_flight Multiprocess metric# TYPE libretranslate_http_requests_in_flight gaugelibretranslate_http_requests_in_flight{api_key="",endpoint="/translate",request_ip="127.0.0.1"} 0.0# HELP libretranslate_http_request_duration_seconds Multiprocess metric# TYPE libretranslate_http_request_duration_seconds summarylibretranslate_http_request_duration_seconds_count{api_key="",endpoint="/translate",request_ip="127.0.0.1",status="200"} 0.0libretranslate_http_request_duration_seconds_sum{api_key="",endpoint="/translate",request_ip="127.0.0.1",status="200"} 0.0
You can then configure prometheus.yml
to read the metrics:
scrape_configs: - job_name: "libretranslate"
# Needed only if you use --metrics-auth-token #authorization: #credentials: "mytoken"
static_configs: - targets: ["localhost:5000"]
To secure the /metrics
endpoint you can also use --metrics-auth-token mytoken
.
If you use Gunicorn, make sure to create a directory for storing multiprocess data metrics and set PROMETHEUS_MULTIPROC_DIR
:
mkdir -p /tmp/prometheus_datarm /tmp/prometheus_data/*export PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_datagunicorn -c scripts/gunicorn_conf.py --bind 0.0.0.0:5000 'wsgi:app(metrics=True)'
Language Bindings
You can use the LibreTranslate API using the following bindings:
- Rust: https://github.com/DefunctLizard/libretranslate-rs
- Node.js: https://github.com/franciscop/translate
- .Net: https://github.com/sigaloid/LibreTranslate.Net
- Go: https://github.com/SnakeSel/libretranslate
- Python: https://github.com/argosopentech/LibreTranslate-py
- PHP: https://github.com/jefs42/libretranslate
- C++: https://github.com/argosopentech/LibreTranslate-cpp
- Swift: https://github.com/wacumov/libretranslate
- Unix: https://github.com/argosopentech/LibreTranslate-sh
- Shell: https://github.com/Hayao0819/Hayao-Tools/tree/master/libretranslate-sh
- Java: https://github.com/suuft/libretranslate-java
- Ruby: https://github.com/noesya/libretranslate
Discourse Plugin
You can use the official discourse translator plugin to translate Discourse topics with LibreTranslate. To install it simply modify /var/discourse/containers/app.yml
:
## Plugins go here## see https://meta.discourse.org/t/19157 for detailshooks: after_code: - exec: cd: $home/plugins cmd: - git clone https://github.com/discourse/docker_manager.git - git clone https://github.com/discourse/discourse-translator ...
Then issue ./launcher rebuild app
. From the Discourse’s admin panel then select “LibreTranslate” as a translation provider and set the relevant endpoint configurations.
See it in action on this page.
Mobile Apps
- LibreTranslator is an Android app available on the Play Store and in the F-Droid store that uses the LibreTranslate API.
- LiTranslate is an iOS app available on the App Store that uses the LibreTranslate API.
Web browser
- minbrowser is a web browser with integrated LibreTranslate support.
- A LibreTranslate Firefox addon is currently a work in progress.