发起你的第一个翻译请求
使用 Node.js 创建并启动内联 JSON 任务,保存西班牙语翻译结果。
将两条英语消息翻译成西班牙语,并保存为 es.json。你需要准备 Node.js 22 或更高版本、一个 Lingo.dev 组织、一个已配置翻译模型的引擎,以及一个具有该引擎翻译访问权限的 API 密钥。
首次请求时,请将引擎配置为直接执行翻译,无需人工审核。等提交和下载流程正常运行后,再添加审核。
设置请求配置 #
在 shell 中设置以下值,填入你自己的组织和引擎 ID。有关如何查找这些 ID,请参阅选择组织和引擎。
sh
export LINGO_API_KEY="YOUR_API_KEY"
export LINGO_ORGANIZATION_ID="YOUR_ORGANIZATION_ID"
export LINGO_ENGINE_ID="YOUR_ENGINE_ID"
export LINGO_REQUEST_ID="welcome-messages-v1-es"LINGO_REQUEST_ID 用于标识本次提交。重试同一请求时,请使用相同的值。如果源内容、语言区域或其他请求选项发生变化,请使用新值。
创建、启动并下载 #
将此程序保存为 translate.mjs:
js
import { writeFile } from "node:fs/promises";
import { setTimeout as delay } from "node:timers/promises";
const origin = "https://api.lingo.dev";
const {
LINGO_API_KEY: apiKey,
LINGO_ORGANIZATION_ID: orgId,
LINGO_ENGINE_ID: engineId,
LINGO_REQUEST_ID: requestId,
} = process.env;
if (!apiKey || !orgId || !engineId || !requestId) {
throw new Error("Set LINGO_API_KEY, LINGO_ORGANIZATION_ID, LINGO_ENGINE_ID, and LINGO_REQUEST_ID");
}
async function request(path, options = {}) {
const response = await fetch(new URL(path, origin), {
...options,
headers: { "X-API-Key": apiKey, ...options.headers },
signal: AbortSignal.timeout(30_000),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response;
}
let job = await (
await request(`/v2/organizations/${encodeURIComponent(orgId)}/localization/jobs`, {
method: "POST",
headers: { "Content-Type": "application/json", "Idempotency-Key": requestId },
body: JSON.stringify({
engineId,
sourceLocale: "en",
targetLocales: ["es"],
documents: [
{
key: "messages.json",
format: { type: "json", options: { context: "Account settings buttons" } },
source: { content: { save: "Save changes", cancel: "Cancel" } },
},
],
}),
})
).json();
await writeFile("translation-job.json", JSON.stringify({ id: job.id, requestId }, null, 2));
console.log(`Created ${job.id}; saved its ID to translation-job.json`);
if (job.uploads.length !== 0) throw new Error("Upload the artifacts listed in job.uploads before starting");
job = await (
await request(`/v2/localization/jobs/${encodeURIComponent(job.id)}/start`, {
method: "POST",
})
).json();
const deadline = Date.now() + 120_000;
while (["Pending", "Running"].includes(job.status._tag)) {
if (Date.now() >= deadline) {
throw new Error(`Still waiting for ${job.id}. Keep its ID and retrieve it again later.`);
}
await delay(2000);
job = await (await request(`/v2/localization/jobs/${encodeURIComponent(job.id)}`)).json();
}
const target = job.documents
.find((document) => document.key === "messages.json")
?.targets.find((item) => item.locale === "es");
if (target?.status._tag !== "Completed") {
throw new Error(`Translation did not complete: ${JSON.stringify(job.status)}`);
}
const translated = await (await request(target.status.downloadUrl)).json();
await writeFile("es.json", JSON.stringify(translated, null, 2) + "\n");
console.log("Saved es.json");运行程序:
sh
node translate.mjs打开 es.json,检查 save 和 cancel,两者都应包含西班牙语文本。API 会在创建任务时存储内联 JSON,但你仍需单独发送 /start 请求。
恢复中断的请求 #
如果程序在创建任务后停止,请从 translation-job.json 中读取 ID,然后获取任务。使用相同的 Idempotency-Key 再次发送相同的创建请求,会返回原来的任务。再次发送 /start 会继续执行同一任务,而不会创建新任务。
轮询超时只会让程序停止等待,不会取消服务器端的处理。请保留任务 ID,稍后再次检查。如果某个目标翻译失败,请先查看其消息和各阶段的结果,再使用新键提交修正后的请求。
接下来,你可以翻译文件、添加 JSON 提示和键控制,或将任务生命周期集成到你的应用中。