JavaScript SDK
KI-Übersetzung mit JavaScript und Lingo.dev
Einführung
Das Lingo.dev JavaScript SDK fügt Webanwendungen, Node.js-Servern und Frontend-Frameworks KI-gestützte Echtzeit-Übersetzung hinzu. Das SDK verarbeitet dynamische Inhalte wie Chat-Nachrichten, Benutzerkommentare und Live-Daten, die sofortige Übersetzung benötigen.
Im Gegensatz zur statischen Datei-Lokalisierung mit dem Lingo.dev CLI verarbeitet das SDK Inhalte bei Bedarf, was es ideal für Chat-Anwendungen, E-Mail-Clients und Social-Media-Tools macht, bei denen sich Inhalte ständig ändern.
Installation
npm install lingo.dev
Grundlegende Einrichtung
Das SDK benötigt einen API-Schlüssel von Lingo.dev.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
Text-Übersetzung
Übersetzen Sie einfache Textstrings in eine Zielsprache.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const result = await lingoDotDev.localizeText("Hello, world!", {
sourceLocale: "en",
targetLocale: "es",
});
console.log(result);
// Output: "¡Hola Mundo!"
Objekt-Übersetzung
Übersetzen Sie verschachtelte Objekte unter Beibehaltung der Struktur. Das SDK verarbeitet rekursiv alle String-Werte.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const content = {
greeting: "Hello",
farewell: "Goodbye",
message: "Welcome to our platform",
};
const translated = await lingoDotDev.localizeObject(content, {
sourceLocale: "en",
targetLocale: "es",
});
console.log(translated);
// Output: { greeting: "Hola", farewell: "Adiós", message: "Bienvenido a nuestra plataforma" }
Batch-Übersetzung in mehrere Sprachen
Übersetzen Sie Inhalte in einem einzigen Aufruf in mehrere Zielsprachen. Gibt ein Array mit einem Ergebnis pro Ziel-Locale zurück.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const results = await lingoDotDev.batchLocalizeText("Hello, world!", {
sourceLocale: "en",
targetLocales: ["es", "fr", "de"],
});
console.log(results);
// Output: ['¡Hola Mundo!', 'Bonjour le monde!', 'Hallo Welt!']
Chat-Übersetzung
Übersetze Chat-Nachrichten unter Beibehaltung der Sprechernamen. Jede Nachricht muss sowohl "name"- als auch "text"-Felder enthalten.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const conversation = [
{ name: "Alice", text: "Hello!" },
{ name: "Bob", text: "How are you?" },
{ name: "Alice", text: "I'm doing well, thanks!" },
];
const translated = await lingoDotDev.localizeChat(conversation, {
sourceLocale: "en",
targetLocale: "es",
});
for (const message of translated) {
console.log(`${message.name}: ${message.text}`);
}
// Output:
// Alice: ¡Hola!
// Bob: ¿Cómo estás?
// Alice: ¡Me va bien, gracias!
HTML-Übersetzung
Übersetze HTML unter Beibehaltung der Markup-Struktur. Erhält alle Tags, Attribute und die Struktur, während nur der Textinhalt übersetzt wird.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const html = "<div>Hello <strong>world</strong></div>";
const translated = await lingoDotDev.localizeHtml(html, {
sourceLocale: "en",
targetLocale: "es",
});
console.log(translated);
// Output: "<div>Hola <strong>mundo</strong></div>"
Fortschrittsüberwachung
Überwache den Übersetzungsfortschritt mit einem Callback. Nützlich für UI-Updates während umfangreicher Übersetzungsvorgänge.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const largeObject = {
page1: "Welcome to our application",
page2: "This is the second page",
page3: "Here is more content",
page4: "Final page of content",
};
await lingoDotDev.localizeObject(
largeObject,
{ sourceLocale: "en", targetLocale: "es" },
(progress) => {
console.log(`Translation progress: ${progress}%`);
},
);
// Output:
// Translation progress: 25%
// Translation progress: 50%
// Translation progress: 75%
// Translation progress: 100%
Übersetzungsparameter
Geschwindigkeit vs. Qualitätskontrolle für zeitkritische Anwendungen.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const result = await lingoDotDev.localizeText("Hello world", {
sourceLocale: "en",
targetLocale: "es",
fast: true, // Prioritize speed over quality
});
console.log(result);
// Output: "Hola mundo"
Konfiguration
Steuerung des Batching-Verhaltens. Das SDK teilt große Payloads basierend auf Elementanzahl und Wortanzahlbeschränkungen auf.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
batchSize: 100, // Max items per API request (default: 50, max: 250)
idealBatchItemSize: 1000, // Target word count per batch (default: 500, max: 2500)
});
const result = await lingoDotDev.localizeText("Configuration test", {
sourceLocale: "en",
targetLocale: "es",
});
console.log(result);
// Output: "Prueba de configuración"
Spracherkennung
Erkennung der Sprache eines Textes. Nur verwenden, wenn die Quellsprache unbekannt ist, da die Erkennung zusätzliche Verarbeitungszeit benötigt.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const locale = await lingoDotDev.recognizeLocale("Bonjour le monde");
console.log(locale);
// Output: 'fr'
Verwendung mit automatischer Erkennung:
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
const result = await lingoDotDev.localizeText("Bonjour le monde", {
sourceLocale: null, // Auto-detect
targetLocale: "en",
});
console.log(result);
// Output: "Hello world"
Fehlerbehandlung
Das SDK beinhaltet automatische Wiederholungsversuche bei Netzwerkproblemen. Implementieren Sie anwendungsspezifische Fehlerbehandlung für andere Fehlerszenarien.
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
try {
const result = await lingoDotDev.localizeText("Hello", {
sourceLocale: "en",
targetLocale: "es",
});
console.log(result);
} catch (error) {
console.error("Translation failed:", error.message);
// Handle error appropriately
}