GT ClassMethodsTranslation

checkFileTranslations

checkFileTranslations 方法的 API 参考:在无需下载文件的情况下检查翻译状态

概览

checkFileTranslations 方法可在不下载文件内容的情况下检查文件的翻译状态。 这对于监控翻译进度、查看完成情况以及判断哪些翻译已可用非常有用。

const gt = new GT({ projectId: 'your-project-id', apiKey: 'your-api-key' });

const status = await gt.checkFileTranslations([
  {
    versionId: 'version-123',
    fileName: 'app.json',
    locale: 'es'
  }
]);

参考资料

参数

名称类型描述
dataFileTranslationQuery[]待检查的文件翻译查询数组
options?CheckFileTranslationsOptions此请求的可选配置

FileTranslationQuery

名称类型描述
versionIdstring要检查的文件版本 ID
fileName?string文件名称标识符(未提供 fileId 时必填)
fileId?string文件 ID 标识符(未提供 fileName 时必填)
localestring用于检查翻译状态的目标 locale

CheckFileTranslationsOptions

名称类型描述
timeout?number请求超时时间(毫秒)

返回值

Promise<CheckFileTranslationsResult> - 包含翻译状态信息。

type CheckFileTranslationsResult = {
  translations: CompletedFileTranslationData[];
}
属性类型描述
translationsCompletedFileTranslationData[]翻译状态数据数组

CompletedFileTranslationData 结构体

type CompletedFileTranslationData = {
  id: string;
  fileId: string;
  versionId: string;
  fileName: string;
  locale: string;
  status: 'completed' | 'pending' | 'failed';
  completedAt: string | null;
  approvedAt: string | null;
  publishedAt: string | null;
  createdAt: string;
  updatedAt: string;
}

示例

基本用法

检查特定 files 和 locales 的翻译状态:

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));
}

注意事项

  • 翻译状态包括 pendingcompletedfailed
  • 已完成的翻译在发布前可能仍需审批
  • 当监控多个翻译任务时,使用此方法可高效检查状态

后续步骤

这份指南怎么样?

checkFileTranslations