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'
  }
]);参考资料
参数
| 名称 | 类型 | 描述 | 
|---|---|---|
| data | FileTranslationQuery[] | 待检查的文件翻译查询数组 | 
| options? | CheckFileTranslationsOptions | 此请求的可选配置 | 
FileTranslationQuery
| 名称 | 类型 | 描述 | 
|---|---|---|
| versionId | string | 要检查的文件版本 ID | 
| fileName? | string | 文件名称标识符(未提供 fileId时必填) | 
| fileId? | string | 文件 ID 标识符(未提供 fileName时必填) | 
| locale | string | 用于检查翻译状态的目标 locale | 
CheckFileTranslationsOptions
| 名称 | 类型 | 描述 | 
|---|---|---|
| timeout? | number | 请求超时时间(毫秒) | 
返回值
Promise<CheckFileTranslationsResult> - 包含翻译状态信息。
type CheckFileTranslationsResult = {
  translations: CompletedFileTranslationData[];
}| 属性 | 类型 | 描述 | 
|---|---|---|
| translations | CompletedFileTranslationData[] | 翻译状态数据数组 | 
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 的翻译状态:
// (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));
}注意事项
- 翻译状态包括 pending、completed和failed
- 已完成的翻译在发布前可能仍需审批
- 当监控多个翻译任务时,使用此方法可高效检查状态
后续步骤
- 查看 Quickstart,获取使用此方法的完整示例
- 查看 downloadTranslatedFile以下载已完成的译文
- 查看 enqueueFiles以创建翻译任务
- 查看 querySourceFile以获取源文件与翻译信息
这份指南怎么样?

