GT ClassMethodsTranslation

getProjectData

getProjectData 方法的 API 参考:用于检索项目信息与配置

概览

getProjectData 方法用于获取某个翻译项目的完整信息,包括其名称、所属组织、默认 locale,以及当前配置的目标 locales。 此方法可用于了解项目配置并校验项目设置。

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

const projectData = await gt.getProjectData('project-123');
console.log(`项目:${projectData.name}`);
console.log(`默认 locale:${projectData.defaultLocale}`);
console.log(`目标 locales:${projectData.currentLocales.join(', ')}`);

参考

参数

名称类型描述
projectIdstring要检索的项目的唯一标识符
options?{ timeout?: number }请求的可选配置

选项

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

返回

Promise<ProjectData> - 包含项目信息和配置信息。

type ProjectData = {
  id: string;
  name: string;
  orgId: string;
  defaultLocale: string;
  currentLocales: string[];
}
属性类型描述
idstring项目的唯一标识符
namestring人类可读的项目名称
orgIdstring拥有该项目的组织标识符
defaultLocalestring项目的默认源 locale
currentLocalesstring[]当前配置的目标 locales 数组

示例

基本用法

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

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

async function getProjectInfo(projectId: string) {
  try {
    const project = await gt.getProjectData(projectId);
    
    console.log('=== 项目信息 ===');
    console.log(`ID:${project.id}`);
    console.log(`名称:${project.name}`);
    console.log(`所属组织:${project.orgId}`);
    console.log(`默认 locale:${project.defaultLocale}`);
    console.log(`目标 locales:${project.currentLocales.join(', ')}`);
    
    return project;
  } catch (error) {
    console.error(`无法获取项目 ${projectId}:`, error);
    throw error;
  }
}

const projectInfo = await getProjectInfo('my-project-123');

说明

  • 该方法仅提供项目信息的只读访问——请使用 dashboard 修改项目设置
  • 此方法需要有效的项目 ID——项目必须可通过提供的 API key 访问
  • 项目数据包含源语言和目标语言的 locale 配置
  • currentLocales 数组表示该项目已配置的所有目标 locales
  • 在启动翻译工作流之前,请使用此方法验证项目配置

后续步骤

这份指南怎么样?

getProjectData