Utilizzo API

Come tradurre con LibreTranslate.

Gli esempi seguenti utilizzano Javascript, ma è possibile utilizzare qualsiasi linguaggio di programmazione. Sono disponibili anche associazioni di linguaggi per molti linguaggi di programmazione.

Traduzione Semplice

Richiesta:

const res = await fetch("https://libretranslate.com/translate", {
  method: "POST",
  body: JSON.stringify({
    q: "Hello!",
    source: "en",
    target: "es",
    api_key: "xxxxxx" // può essere facoltativo con l'auto-hosting
  }),
  headers: { "Content-Type": "application/json" },
});

console.log(await res.json());

Risposta:

{
    "translatedText": "¡Hola!"
}
Nota

Il parametro api_key è richiesto solo quando si utilizza un’istanza configurata con chiavi API, come libretranslate.com. Le istanze auto-ospitate non lo richiedono.

Rilevamento Automatico della Lingua di Origine

Richiesta:

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());

Risposta:

{
    "detectedLanguage": {
        "confidence": 90.0,
        "language": "fr"
    },
    "translatedText": "Hello!"
}

Tradurre HTML/Markup

Richiesta:

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());

Risposta:

{
    "translatedText": "<p class=\"green\">¡Hola!</p>"
}

Traduzioni Alternative

Richiesta:

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());

Risposta:

{
    "alternatives": [
        "Salve",
        "Pronto"
    ],
    "translatedText": "Ciao"
}

Rilevamento Lingua

Richiesta:

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());

Risposta:

[{
    "confidence": 90.0,
    "language": "fr"
}]

Lingue Supportate

https://libretranslate.com/languages

Interfacce di Binding

È possibile utilizzare l’API di LibreTranslate utilizzando i seguenti bindings: