Utilisation de l'API
Les exemples ci-dessous utilisent Javascript, mais vous pouvez utiliser n’importe quel langage de programmation. Des bindings pour de nombreux langages de programmation sont également disponibles.
Traduction simple
Section intitulée « Traduction simple »Requête :
const res = await fetch("https://libretranslate.com/translate", { method: "POST", body: JSON.stringify({ q: "Hello!", source: "en", target: "es", api_key: "xxxxxx" // peut être optionnel avec l'auto-hébergé }), headers: { "Content-Type": "application/json" },});
console.log(await res.json());Réponse :
{ "translatedText": "¡Hola!"}Détection automatique de la langue source
Section intitulée « Détection automatique de la langue source »Requête :
const res = await fetch("https://libretranslate.com/translate", { method: "POST", body: JSON.stringify({ q: "Bonjour!", source: "auto", target: "en", api_key: "xxxxxx" }), headers: { "Content-Type": "application/json" },});
console.log(await res.json());Réponse :
{ "detectedLanguage": { "confidence": 90.0, "language": "fr" }, "translatedText": "Hello!"}Traduire du HTML/balisage
Section intitulée « Traduire du HTML/balisage »Requête :
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", api_key: "xxxxxx" }), headers: { "Content-Type": "application/json" },});
console.log(await res.json());Réponse :
{ "translatedText": "<p class=\"green\">¡Hola!</p>"}Traductions alternatives
Section intitulée « Traductions alternatives »Requête :
const res = await fetch("https://libretranslate.com/translate", { method: "POST", body: JSON.stringify({ q: "Hello", source: "en", target: "it", format: "text", alternatives: 3, api_key: "xxxxxx" }), headers: { "Content-Type": "application/json" },});
console.log(await res.json());Réponse :
{ "alternatives": [ "Salve", "Pronto" ], "translatedText": "Ciao"}Détection de langue
Section intitulée « Détection de langue »Requête :
const res = await fetch("https://libretranslate.com/detect", { method: "POST", body: JSON.stringify({ q: "Bonjour!", }), headers: { "Content-Type": "application/json" },});
console.log(await res.json());Réponse :
[{ "confidence": 90.0, "language": "fr"}]Langues supportées
Section intitulée « Langues supportées »https://libretranslate.com/languages
Bindings de langage
Section intitulée « Bindings de langage »Vous pouvez utiliser l’API LibreTranslate en utilisant les bindings suivants :
- Rust : https://github.com/DefunctLizard/libretranslate-rs
- Node.js : https://github.com/franciscop/translate
- TypeScript : https://github.com/tderflinger/libretranslate-ts
- .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/stokito/libretranslate-java
- Ruby : https://github.com/noesya/libretranslate
- R : https://github.com/myanesp/libretranslateR