JavaScript SDK
KI-Übersetzung mit JavaScript und Lingo.dev
Einführung
Das Lingo.dev JavaScript SDK fügt Echtzeit-KI-gestützte Übersetzung zu Webanwendungen, Node.js-Servern und Frontend-Frameworks hinzu. Das SDK verarbeitet dynamische Inhalte wie Chat-Nachrichten, Benutzerkommentare und Live-Daten, die sofortige Übersetzung benötigen.
Im Gegensatz zur statischen Dateilokalisierung mit der Lingo.dev CLI verarbeitet das SDK Inhalte on-demand, 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 Textzeichenfolgen 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 Zeichenfolgenwerte.
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" }
Stapelübersetzung in mehrere Sprachen
Übersetzen Sie Inhalte in mehrere Zielsprachen mit einem einzigen Aufruf. Gibt ein Array mit einem Ergebnis pro Zielsprache 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
Übersetzen Sie Chat-Nachrichten unter Beibehaltung der Sprechernamen. Jede Nachricht muss sowohl die Felder "name" als auch "text" 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
Übersetzen Sie HTML unter Beibehaltung des Markups. Behält alle Tags, Attribute und die Struktur bei, während nur Textinhalte übersetzt werden.
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>"
Fortschrittsverfolgung
Überwachen Sie den Übersetzungsfortschritt mit einem Callback. Nützlich für die Aktualisierung der Benutzeroberfläche 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
Steuern Sie das Batching-Verhalten. Das SDK teilt große Payloads basierend auf Einträgen und Wortanzahl-Beschrä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
Erkennen Sie die Sprache einer Textzeichenfolge. Verwenden Sie dies nur, wenn die Ausgangssprache unbekannt ist, da die Erkennung zusätzliche Verarbeitungszeit erfordert.
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 enthält automatische Wiederholungsversuche bei Netzwerkproblemen. Implementieren Sie eine Fehlerbehandlung auf Anwendungsebene für andere Fehler.
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
}