GT ClassMethodsTranslation
enqueueFiles
enqueueFiles 方法的 API 参考:将文件翻译任务入队
概览
enqueueFiles 方法会在 General Translation API 中,为先前上传的 files 排入队列以创建翻译任务。
该方法接收文件引用,并为指定的目标 locales 创建翻译任务。
const gt = new GT({ projectId: 'your-project-id', apiKey: 'your-api-key' });
const result = await gt.enqueueFiles(fileRefs, {
sourceLocale: 'en',
targetLocales: ['es', 'fr', 'de']
});仅在上传完成后,才能将 files 加入翻译队列。
参考资料
参数
| 名称 | 类型 | 描述 |
|---|---|---|
files | FileUploadRef[] | 之前上传的文件引用数组 |
options | EnqueueOptions | 翻译任务的配置选项 |
EnqueueOptions
| 名称 | 类型 | 描述 |
|---|---|---|
sourceLocale? | string | 翻译的源语言(sourceLocale)(默认取实例的 sourceLocale) |
targetLocales? | string[] | 翻译的目标语言数组(默认取实例的 targetLocale) |
publish? | boolean | 完成后是否自动发布译文 |
requireApproval? | boolean | 发布前译文是否需要审批 |
modelProvider? | string | 指定用于翻译的 AI 模型提供商 |
force? | boolean | 即使已有译文也强制重新翻译 |
timeout? | number | 请求超时时间(毫秒) |
返回
Promise<EnqueueFilesResult> - 包含任务信息和处理细节。
type EnqueueFilesResult = {
translations: CompletedFileTranslationData[];
data: Record<string, { fileName: string; versionId: string }>;
locales: string[];
message: string;
}| 属性 | 类型 | 描述 |
|---|---|---|
translations | CompletedFileTranslationData[] | 翻译任务数据数组 |
data | Record<string, { fileName: string; versionId: string }> | 将文件标识符映射到文件元数据的映射 |
locales | string[] | 这些翻译任务的目标 locale 列表 |
message | string | 来自 API 的状态消息 |
示例
// (1) 创建 GT 实例
const targetLocales = ['es', 'fr', 'de'];
const gt = new GT({
projectId: 'your-project-id',
apiKey: 'your-api-key',
});
// (2) 上传源文件
const fileUpload = {
content: fileContents,
fileName: filePath,
fileFormat: 'JSON',
locale: 'en',
};
const files = [ { source: fileUpload } ];
const { uploadedFiles } = await gt.uploadSourceFiles(
files,
{ sourceLocale: 'en' }
);
// (3) 将文件翻译任务加入队列
const enqueueResult = await gt.enqueueFiles(
uploadedFiles,
{
sourceLocale: 'en',
targetLocales: targetLocales,
});
// (4) 等待所有翻译完成
let translationIds = [];
const statusQuery = Object.values(enqueueResult.data).flatMap(({fileName, versionId}) => {
return targetLocales.map((locale) => ({locale, fileName, versionId}));
});
while (true) {
const status = await gt.checkFileTranslations(statusQuery);
if (status.translations.length === statusQuery.length) {
translationIds = status.translations.map(translation => translation.id);
break;
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
// (5) 批量下载文件
const result = await gt.downloadFileBatch(translationIds);注意事项
- 必须先使用
uploadSourceFiles上传文件内容 - 翻译作业为异步执行——请使用
checkFileTranslations监控进度
后续步骤
- 查看
uploadSourceFiles以在入队前上传文件 - 查看
checkFileTranslations以监控翻译进度 - 查看
downloadTranslatedFile以获取已完成的译文
本指南如何?