# General Translation Integrations: 快速入门
URL: https://generaltranslation.com/zh/docs/integrations/paraglide/quickstart.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 使用 General Translation 翻译你的 Paraglide JS 消息文件，并在 CI 中让译文保持同步。

你可以继续使用 Paraglide JS 作为国际化库。CLI 负责翻译你的消息文件 (例如将 `messages/en.json` 翻译为 `messages/de.json`) ，再由 Paraglide JS 将其编译为你现有代码中已在调用的类型化消息函数。译文始终保存在你的代码仓库中。

## 工作原理 [#how-it-works]

* [`gt translate`](/docs/cli/reference/commands/translate) 会读取基础区域设置的消息文件，并在同一目录下为每个目标区域设置生成对应的翻译后的文件。
* 只翻译文本内容。消息键、`{count}` 等占位符、[标记](https://paraglidejs.com/markup)、嵌套的消息对象，以及复数形式等[变体](https://paraglidejs.com/variants)均保持原有结构不变。
* 翻译会遵循分配给项目的[上下文组](/docs/platform/dashboard/guides/defining-context-for-translations)中的词汇表和自定义提示词，确保产品名称和术语前后一致。
* 源消息有变动时，随时可在本地或 CI 中重新运行。启用 [`saveLocal`](/docs/cli/reference/config#save-local) 后，你对[受支持的消息字符串](#supported-messages)所做的修改会在后续运行中保留。
* 应用代码和 Paraglide JS 配置均无需改动。

```json title="messages/en.json"
{
  "$schema": "https://inlang.com/schema/inlang-message-format",
  "title": "Welcome back, {name}!",
  "nav": {
    "account": {
      "settings": "Settings",
      "sign_out": "Sign out"
    }
  },
  "cart_items": [
    {
      "declarations": ["input count", "local countPlural = count: plural"],
      "selectors": ["countPlural"],
      "match": {
        "countPlural=one": "You have {count} item in your cart",
        "countPlural=*": "You have {count} items in your cart"
      }
    }
  ]
}
```

```json title="messages/de.json"
{
  "$schema": "https://inlang.com/schema/inlang-message-format",
  "title": "Willkommen zurück, {name}!",
  "nav": {
    "account": {
      "settings": "Einstellungen",
      "sign_out": "Abmelden"
    }
  },
  "cart_items": [
    {
      "declarations": ["input count", "local countPlural = count: plural"],
      "selectors": ["countPlural"],
      "match": {
        "countPlural=one": "Sie haben {count} Artikel in Ihrem Warenkorb",
        "countPlural=*": "Sie haben {count} Artikel in Ihrem Warenkorb"
      }
    }
  ]
}
```

## 开始之前 [#before-start]

你需要：

* Node.js 20 或更高版本。
* 一个 Paraglide JS 项目，且其消息采用 inlang 消息格式 (即 `project.inlang/settings.json` 中的 `plugin.inlang.messageFormat`) ，例如 `messages/en.json`。
* 一个 General Translation 账户。免费组织会从滚动发放的平台使用额度中扣费，因此无需绑定付款方式即可开始使用 (参见[管理计费](/docs/platform/dashboard/guides/managing-billing)) 。

## 快速入门 [#quickstart]

配置 CLI，为消息文件添加规则，然后执行翻译并检查结果。

### 1. 设置 CLI

将 `gt` 安装为开发依赖：

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

  <Tab value="yarn">
    ```bash
    yarn add --dev gt
    ```
  </Tab>

  <Tab value="bun">
    ```bash
    bun add --dev gt
    ```
  </Tab>

  <Tab value="pnpm">
    ```bash
    pnpm add --save-dev gt
    ```
  </Tab>
</Tabs>

然后运行 [`gt init`](/docs/cli/reference/commands/init)：

```bash
npx gt init --no-react-setup --file-patterns "json=messages/[locale].json" --dev-credentials
```

按照提示登录并选择语言。使用 `project.inlang/settings.json` 中的 `baseLocale` 作为默认区域设置，并从其 `locales` 中选择目标区域设置。如果你的消息包含复数形式，请先查看[支持的消息](#supported-messages)。

* `--no-react-setup` 不会改动应用的代码。如果初始化设置过程中提示安装 `gt-react`，请跳过：渲染消息的工作仍由 Paraglide JS 负责。
* `--file-patterns` 用于告诉 CLI 消息文件的位置。它对应 `project.inlang/settings.json` 中的 `pathPattern`，只是将 `{locale}` 换成了 `[locale]`；如果你的消息存放在其他位置，请相应修改。
* `--dev-credentials` 会创建或选择一个项目，并将其 ID 保存到 `.env.local`。

<Callout type="info">
  **编程智能体：**[`gt init`](/docs/cli/reference/commands/init) 需要用户已登录。请运行 [`gt login --no-browser`](/docs/cli/reference/commands/login)，并让用户批准该命令输出的登录链接。在上面的 [`gt init`](/docs/cli/reference/commands/init) 命令中添加 `--json` 以获得结构化输出并以非交互方式完成初始化设置，同时加上 `--default-locale` 和 `--locales`。使用 `--project-id` 复用现有项目；仅在需要新建项目时才使用 `--create-project --project-name <name>` (如果用户属于多个组织，还需加上 `--org-id`) 。[`gt init`](/docs/cli/reference/commands/init) 会列出所有仍缺少的选项。
</Callout>

### 2. 配置消息文件

将下方的 `options` block 合并到 [`gt init`](/docs/cli/reference/commands/init) 创建的 `gt.config.json` 中，并保留其中原有的 `defaultLocale`、`locales` 和 `files`。

如果你的消息文件存放在其他位置，还需修改 `jsonSchema` 中的键，使其与这些文件的路径相匹配。例如，若使用 `json=locales/[locale].json`，则需将下方的 `"messages/*.json"` 替换为 `"locales/*.json"`。该规则仅对路径与其键相匹配的文件生效。

```json title="gt.config.json"
{
  "defaultLocale": "en",
  "locales": ["de", "fr"],
  "files": {
    "json": {
      "include": ["messages/[locale].json"]
    }
  },
  "options": {
    "saveLocal": true,
    "jsonSchema": {
      "messages/*.json": {
        "include": [
          "$..[?(typeof @ === 'string' && @property !== '$schema' && !(typeof @property === 'number' && (@parentProperty === 'declarations' || @parentProperty === 'selectors')))]"
        ]
      }
    }
  }
}
```

* `jsonSchema` 会选中每条消息的文本进行翻译，包括嵌套消息及其每个变体。它会跳过 `$schema` URL 以及变体的 `declarations` 和 `selectors`，从而保持消息结构不变。
* [`saveLocal`](/docs/cli/reference/config#save-local) 会在后续运行中保留你对[受支持的消息字符串](#supported-messages)所做的编辑。

<Callout type="warn">
  对于 inlang 消息文件，请保留 `jsonSchema` 规则。否则，变体的 declarations 和 selectors (例如 `input count`) 可能会被翻译，导致 Paraglide JS 无法编译翻译后的文件。
</Callout>

### 3. 翻译消息

<Callout type="warn">
  首次运行会根据源消息重写每个目标区域设置文件，文件中已有的译文也会被覆盖。请先提交消息文件，然后审校差异，恢复需要保留的现有译文。
</Callout>

运行 [`gt translate`](/docs/cli/reference/commands/translate)：

```bash
npx gt translate
```

CLI 会为每个目标区域设置写入一个消息文件 (例如 `messages/de.json`) ，并生成一个 `gt-lock.json` 文件，用于记录已下载的译文，以便后续运行时检测到你所做的修改。Paraglide JS 会像处理其他消息文件一样编译这些新文件。

使用 inlang CLI (3.4.0 或更高版本) 检查翻译后的文件：

```bash
npx @inlang/cli check --project ./project.inlang
```

该检查会报告缺失的译文、缺失的变量以及未使用的消息。请提交你的消息文件和 `gt-lock.json`。

### 4. 在 CI 中翻译

在构建之前运行 [`gt translate`](/docs/cli/reference/commands/translate)，这样每次推送时，新增和变更的消息都会自动翻译。在你的 CI 服务中添加两个密钥：

* `GT_API_KEY`：具有 **Files &gt; Write**、**Translation queue &gt; Enabled** 和 **Context &gt; Write** 权限的[项目 API 密钥](/docs/platform/dashboard/reference/api-keys#create-project-keys)。`.env.local` 中的开发密钥无法运行翻译 (参见[在 CI 中使用显式密钥](/docs/cli/guides/configuring#credentials)) 。
* `GT_PROJECT_ID`：由 [`gt init`](/docs/cli/reference/commands/init) 保存到 `.env.local` 中的项目 ID。

```yaml title=".github/workflows/build.yml"
- run: npx gt translate
  env:
    GT_API_KEY: ${{ secrets.GT_API_KEY }}
    GT_PROJECT_ID: ${{ secrets.GT_PROJECT_ID }}
- run: npx @inlang/cli check --project ./project.inlang
```

请仅在可以读取密钥的工作流中运行此步骤，例如推送到主分支时触发的工作流，而不要在来自 fork 的拉取请求中运行。请通过拉取请求提交更新后的消息文件和 `gt-lock.json`，以便在译文发布前进行审校。

## 支持的消息 [#supported-messages]

CLI 可翻译以下 inlang 消息格式的消息：

* 纯文本消息和包含占位符的消息
* [标记](https://paraglidejs.com/markup)，例如链接和强调
* 嵌套的消息对象
* [变体](https://paraglidejs.com/variants)，例如复数形式

源消息中的每个复数变体都应以 `*` 分支结尾，例如使用 `countPlural=*`，而不是 `countPlural=other`。如果目标语言用到了源消息中没有的复数形式 (例如法语和西班牙语中用于大整数的形式) ，Paraglide JS 会回退到 `*` 分支。

### 复数形式更多的语言

如果目标语言所需的复数形式比源语言多，这类复数消息目前暂不支持。例如，俄语、乌克兰语、波兰语、捷克语和阿拉伯语在表达日常数字时，会用到英语中没有的额外形式。CLI 不会生成这些形式，也不会保留你手动添加的形式，因此这些消息在遇到某些数字时语法会出错，并且无法通过 `inlang check`。

如果你的消息文件包含复数形式，请暂时不要将这些语言添加到 `gt.config.json` 的 `locales` 中。如果不含复数形式，这些语言的用法与其他语言无异。

## Next steps

- /docs/integrations/paraglide/guides/adding-source-context
- /docs/integrations/paraglide/guides/resolving-imports
- /docs/integrations/paraglide/guides/adding-file-formats

## Sitemap

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