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 加入翻译队列。

参考资料

参数

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

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;
}
属性类型描述
translationsCompletedFileTranslationData[]翻译任务数据数组
dataRecord<string, { fileName: string; versionId: string }>将文件标识符映射到文件元数据的映射
localesstring[]这些翻译任务的目标 locale 列表
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);

注意事项

后续步骤

本指南如何?