Uso de la API
Los siguientes ejemplos usan Javascript, pero puedes usar cualquier lenguaje de programación. También hay enlaces de lenguaje disponibles para muchos lenguajes de programación.
Traducción Simple
Sección titulada «Traducción Simple»Solicitud:
const res = await fetch("https://libretranslate.com/translate", { method: "POST", body: JSON.stringify({ q: "Hello!", source: "en", target: "es", api_key: "xxxxxx" // puede ser opcional si lo alojas tú mismo }), headers: { "Content-Type": "application/json" },});
console.log(await res.json());Respuesta:
{ "translatedText": "¡Hola!"}Detección Automática del Idioma de Origen
Sección titulada «Detección Automática del Idioma de Origen»Solicitud:
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());Respuesta:
{ "detectedLanguage": { "confidence": 90.0, "language": "fr" }, "translatedText": "Hello!"}Traducir HTML/Marcado
Sección titulada «Traducir HTML/Marcado»Solicitud:
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());Respuesta:
{ "translatedText": "<p class=\"green\">¡Hola!</p>"}Traducciones Alternativas
Sección titulada «Traducciones Alternativas»Solicitud:
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());Respuesta:
{ "alternatives": [ "Salve", "Pronto" ], "translatedText": "Ciao"}Detección de Idioma
Sección titulada «Detección de Idioma»Solicitud:
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());Respuesta:
[{ "confidence": 90.0, "language": "fr"}]Idiomas Soportados
Sección titulada «Idiomas Soportados»https://libretranslate.com/languages
Enlaces de Lenguaje
Sección titulada «Enlaces de Lenguaje»Puedes usar la API de LibreTranslate con los siguientes enlaces:
- 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