JavaScript SDK
使用 JavaScript 和 Lingo.dev 实现 AI 翻译
简介
Lingo.dev JavaScript SDK 为 Web 应用、Node.js 服务器和前端框架提供实时 AI 翻译能力。该 SDK 可处理如聊天消息、用户评论和需要即时翻译的动态数据。
与使用 Lingo.dev CLI 进行静态文件本地化不同,SDK 支持按需处理内容,非常适合内容频繁变化的聊天应用、邮件客户端和社交媒体工具。
安装
npm install lingo.dev
基本配置
SDK 需要从 Lingo.dev 获取 API 密钥。
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: process.env.LINGODOTDEV_API_KEY,
});
文本翻译
将简单文本字符串翻译为目标语言。
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!"
对象翻译
在保留结构的同时翻译嵌套对象。SDK 会递归处理所有字符串值。
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" }
批量翻译为多种语言
一次调用即可将内容翻译为多个目标语言。返回的数组中每个元素对应一个目标语言的结果。
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!']
聊天翻译
在保留发言人姓名的同时翻译聊天消息。每条消息必须包含 "name" 和 "text" 字段。
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 翻译
在保留标记的同时翻译 HTML。仅翻译文本内容,保持所有标签、属性和结构不变。
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>"
进度跟踪
通过回调函数监控翻译进度。适用于在大规模翻译操作期间实时更新 UI。
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%
翻译参数
针对对时效性有要求的应用,支持速度与质量的调节。
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"
配置
可控制批处理行为。SDK 会根据条目数和词数限制自动拆分大体量数据。
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"
语言检测
检测文本字符串的语言。仅在源语言未知时使用,因为检测会增加处理时间。
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'
与自动检测配合使用:
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"
错误处理
SDK 内置网络问题的自动重试机制。对于其他类型的失败,请实现应用层错误处理。
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
}