GT ClassMethodsTranslation

enqueueFiles

enqueueFiles 方法的 API 参考:将文件翻译任务加入队列

概述

enqueueFiles 方法会为此前上传到 General Translation API 的文件加入翻译任务队列。 该方法接收文件引用,并为指定的目标 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']
});

只有在上传完成后,才能将文件加入翻译队列。

参考

参数

名称类型描述
filesFileUploadRef[]来自先前已上传文件的文件引用数组
optionsEnqueueOptions翻译任务的配置选项

EnqueueOptions

名称类型描述
sourceLocale?string翻译的源语言(默认为实例的 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;
}
属性类型描述
translationsCompletedFileTranslationData[]翻译任务数据数组
dataRecord<string, { fileName: string; versionId: string }>文件标识符到文件元数据的映射
localesstring[]翻译任务的目标 locales 列表
messagestring来自 API 的状态消息

示例

index.ts
// (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);

注意事项

后续步骤

这份指南怎么样?

enqueueFiles