GT ClassMethodsTranslation

querySourceFile

querySourceFile 方法的 API 参考:获取源文件和翻译信息

概览

querySourceFile 方法用于检索某个源文件及其所有关联译文的完整信息。 其中包括文件元数据、所有 locale 的翻译状态,以及创建、完成、审批和发布的时间戳。

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

const result = await gt.querySourceFile({
  fileId: 'file-123',
  versionId: 'version-456'
});

console.log(`源文件:${result.sourceFile.fileName}`);
console.log(`支持 ${result.translations.length} 种语言环境`);

参考资料

参数

名称类型描述
dataFileQuery指定要获取的文件的文件查询对象
options?CheckFileTranslationsOptions请求的可选配置项

FileQuery

名称类型描述
fileIdstring要查询的文件的唯一标识符
versionId?string该文件特定版本的可选 versionId

CheckFileTranslationsOptions

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

返回值

Promise<FileQueryResult> - 包含源文件信息以及所有 locale 的翻译状态。

type FileQueryResult = {
  sourceFile: {
    id: string;
    fileId: string;
    versionId: string;
    sourceLocale: string;
    fileName: string;
    fileFormat: string;
    dataFormat: string | null;
    createdAt: string;
    updatedAt: string;
    approvalRequiredAt: string | null;
    locales: string[];
  };
  translations: {
    locale: string;
    completedAt: string | null;
    approvedAt: string | null;
    publishedAt: string | null;
    createdAt: string | null;
    updatedAt: string | null;
  }[];
}

源文件属性

属性类型说明
idstring内部数据库 ID
fileIdstring唯一文件标识符
versionIdstring版本标识符
sourceLocalestring源语言(sourceLocale)
fileNamestring原始文件名
fileFormatstring文件格式(JSON、MD、MDX 等)
dataFormatstring | null文件中的数据格式(ICU、i18next、JSX)
createdAtstring文件创建的 ISO 时间戳
updatedAtstring上次更新的 ISO 时间戳
approvalRequiredAtstring | null请求审批的 ISO 时间戳
localesstring[]此文件的目标 locale 列表

翻译属性

属性类型描述
localestring目标语言代码
completedAtstring | null翻译完成的 ISO 时间戳
approvedAtstring | null翻译审核通过的 ISO 时间戳
publishedAtstring | null翻译发布的 ISO 时间戳
createdAtstring | null翻译任务创建的 ISO 时间戳
updatedAtstring | null最近一次翻译更新的 ISO 时间戳

示例

index.ts
import { GT } from 'generaltranslation';

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

async function getFileInfo(fileId: string, versionId?: string) {
  const result = await gt.querySourceFile({
    fileId,
    versionId
  });
  
  console.log('=== 源文件信息 ===');
  console.log(`名称:${result.sourceFile.fileName}`);
  console.log(`格式:${result.sourceFile.fileFormat}`);
  console.log(`源语言(sourceLocale):${result.sourceFile.sourceLocale}`);
  console.log(`创建时间:${new Date(result.sourceFile.createdAt).toLocaleString()}`);
  console.log(`更新时间:${new Date(result.sourceFile.updatedAt).toLocaleString()}`);
  
  console.log('\n=== 翻译状态 ===');
  result.translations.forEach(translation => {
    console.log(`${translation.locale}:`);
    console.log(`  创建时间:${translation.createdAt ? new Date(translation.createdAt).toLocaleString() : '未开始'}`);
    console.log(`  完成时间:${translation.completedAt ? new Date(translation.completedAt).toLocaleString() : '进行中'}`);
    console.log(`  发布时间:${translation.publishedAt ? new Date(translation.publishedAt).toLocaleString() : '未发布'}`);
  });
  
  return result;
}

const fileInfo = await getFileInfo('file-123', 'version-456');

说明

  • 返回所有目标 locale 的源文件与翻译状态
  • 翻译时间戳遵循以下生命周期:createdAtcompletedAtapprovedAtpublishedAt
  • 时间戳为 null 表示尚未进入该阶段
  • 源文件中的 locales 数组显示已配置用于翻译的所有目标 locale
  • 使用此方法进行详细报告、进度追踪和文件管理流程

后续步骤

本指南如何?