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项目的默认源语言(defaultLocale)
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
  • 在启动翻译工作流之前,请使用此方法验证项目配置

后续步骤

本指南如何?