# General Translation Platform: 快速入门
URL: https://generaltranslation.com/zh/docs/platform/core/quickstart.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 当你需要更底层的控制时，使用 General Translation Core 库，并翻译你的第一个字符串。

`generaltranslation` 库是 General Translation 的 Core i18n 库，提供翻译、格式化和区域设置工具等功能。

大多数框架应用都应从 [gt-react 或 gt-next](/docs/react/overview) 开始。如果你需要更底层的控制、自定义工作流，或在框架集成之外使用翻译工具，请直接使用 `generaltranslation`。

<Video src="https://assets.gtx.dev/core-demo.mp4" />

## `generaltranslation` 的功能 [#what-it-does]

`generaltranslation` 让你可以直接使用：

* **String translation**，用于在代码中翻译字符串或结构化内容。
* **文件翻译**，用于上传源文件、将翻译任务加入队列、检查状态，以及下载翻译后的输出。
* **区域设置工具**，用于验证、比较、解析和显示区域设置代码。
* **格式化工具**，用于按区域设置格式化数字、日期、消息、列表和相对时间。

## 何时使用 `generaltranslation` [#when-to-use]

当你需要以下功能时，请使用 `generaltranslation`：

* 在脚本、后端服务、worker 或自定义构建步骤中翻译内容。
* 在不使用 CLI 的情况下上传和下载翻译文件。
* 围绕项目文件、翻译任务、分支或标签构建自定义翻译自动化。
* 在 React 或 Next.js 之外格式化数字、日期、消息 或区域设置名称。
* 在使用不同前端框架的包之间共享 GT 的行为。

## 快速入门 [#quickstart]

安装 `generaltranslation`，使用你的项目 ID 和 API 密钥 创建一个 [GT](/docs/platform/core/reference/gt-class/constructor) 实例，并翻译第一条字符串。

### 1. 安装 `generaltranslation`

<Tabs items={['npm', 'yarn', 'bun', 'pnpm']}>
  <Tab value="npm">
    ```bash
    npm install generaltranslation
    ```
  </Tab>

  <Tab value="yarn">
    ```bash
    yarn add generaltranslation
    ```
  </Tab>

  <Tab value="bun">
    ```bash
    bun add generaltranslation
    ```
  </Tab>

  <Tab value="pnpm">
    ```bash
    pnpm add generaltranslation
    ```
  </Tab>
</Tabs>

### 2. 获取 API 密钥

在仪表板中创建 `GT_PROJECT_ID` 和 `GT_API_KEY`。打开 [API Keys 页面](https://dash.generaltranslation.com/api-keys)，点击 **Create API Key**，输入名称并确认。

```bash
GT_PROJECT_ID=your-project-id
GT_API_KEY=your-api-key
```

General Translation 为个人项目、独立开发者和社区提供较为宽松的免费速率限制。

### 3. 初始化 GT 类

初始化 [GT](/docs/platform/core/reference/gt-class/constructor) 类，并安全地传入 `GT_PROJECT_ID` 和 `GT_API_KEY`。

```typescript title="src/index.ts"
import { GT } from 'generaltranslation';

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

你可以在 [GT](/docs/platform/core/reference/gt-class/constructor) 实例上配置默认区域设置。当大多数调用都使用相同的 source 或目标区域设置时，可以使用默认区域设置。

```typescript title="src/index.ts"
const gt = new GT({
  projectId: 'your-project-id',
  apiKey: 'your-api-key',
  sourceLocale: 'en',
  targetLocale: 'es',
});
```

 (有关构造函数的所有选项，请参阅 [GT 类](/docs/platform/core/reference/gt-class/constructor)) 。

### 4. 翻译你的第一个字符串

调用 [translate](/docs/platform/core/reference/gt-class-methods/translation/translate) 方法来翻译第一个字符串。传入要翻译的字符串和目标区域设置。

```typescript title="src/index.ts"
const result = await gt.translate('Hello, world!', 'es');

if (result.success) {
  console.log(result.translation); // "¡Hola, mundo!"
} else {
  console.error(`Translation failed: ${result.error}`);
}
```

翻译方法需要 API 密钥 和 项目 ID。格式化和区域设置工具无需访问 translation API 也可使用。

## Next steps

- /docs/platform/core/guides/translating-strings
- /docs/platform/core/guides/translating-files
- /docs/platform/core/guides/locale-codes

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
