# General Translation Integrations: paraglide-context
URL: https://generaltranslation.com/zh/docs/integrations/paraglide/reference/paraglide-context.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 将每处使用 Paraglide JS 消息的位置周围的源代码写入元数据文件。paraglide-context 命令的 API 参考。

该命令随 `@generaltranslation/paraglide-js` 一同安装。每次运行会分析应用、写入元数据文件，然后退出。请在运行 [`gt translate`](/docs/cli/reference/commands/translate) 之前先运行此命令。

## 概览 [#overview]

```bash
npx paraglide-context [root] [options]
```

`root` 是项目本身或项目中的某个目录，默认为当前工作目录。标志接收的所有路径均相对于项目根目录，即从 `root` 开始向上查找到的最近一个 `package.json` 所在的目录。

[`collectContext`](/docs/integrations/paraglide/reference/collect-context) 可在代码中实现相同的功能，且接受相同的选项。

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

* 选取源文件，解析其中的导入语句，并找出 Paraglide JS 消息的所有使用位置。 (参见[用法检测](/docs/integrations/paraglide/reference/usage-detection)。) 
* 在每个基础区域设置的消息文件旁写入一个元数据文件，例如 `messages/en.metadata.json`。 (参见[元数据文件](/docs/integrations/paraglide/reference/metadata-files)。) 
* 不会运行应用的打包工具。导入别名会从 `tsconfig.json`、`package.json` 以及 Vite、webpack 或 rspack 配置中读取。
* 对所有跳过的内容输出警告。警告不会影响退出码。

## 标志 [#flags]

每个标志对应 [`collectContext`](/docs/integrations/paraglide/reference/collect-context#options) 的一个选项，其具体行为请参阅该处的完整说明。标记为*可重复*的标志可多次使用，以传入多个值。

| 参数                    | 描述                                                                                                                          | 类型         | 可选 | 默认      |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------- | ---------- | -- | ------- |
| `[root]`              | 项目，或项目中的某个目录。设置 [`root`](/docs/integrations/paraglide/reference/collect-context#root)。                                      | `string`   | 是  | 工作目录    |
| `--include <path>`    | 要分析的路径或 glob。可重复。设置 [`include`](/docs/integrations/paraglide/reference/collect-context#include)。                            | `string[]` | 是  | 整个项目    |
| `--exclude <path>`    | 要跳过的路径或 glob。可重复。设置 [`exclude`](/docs/integrations/paraglide/reference/collect-context#exclude)。                            | `string[]` | 是  | 测试和工具配置 |
| `--outdir <dir>`      | 直接使用 (不经验证) 的 Paraglide JS 输出目录。可重复。设置 [`outdirs`](/docs/integrations/paraglide/reference/collect-context#outdirs)。         | `string[]` | 是  | 自动检测    |
| `--project <dir>`     | 需要为其消息生成元数据文件的 inlang 项目。可重复。设置 [`inlangProjects`](/docs/integrations/paraglide/reference/collect-context#inlang-projects)。 | `string[]` | 是  | 自动检测    |
| `--alias <name=path>` | 导入别名，例如 `~=./src`。以 `.` 开头的替换路径相对于项目根目录解析。可重复。设置 [`alias`](/docs/integrations/paraglide/reference/collect-context#alias)。   | `string[]` | 是  | —       |
| `--no-bundler-config` | 不从打包工具配置中读取别名。将 [`bundlerConfigs`](/docs/integrations/paraglide/reference/collect-context#bundler-configs) 设为 `[]`。         | `boolean`  | 是  | `false` |
| `--radius <lines>`    | 每处用法上下各取的上下文行数，须为非负整数。设置 [`radius`](/docs/integrations/paraglide/reference/collect-context#radius)。                         | `number`   | 是  | `10`    |
| `--verbose`           | 打印摘要：找到的用法、分析过的文件、使用的输出目录，以及从中读取别名的配置文件。                                                                                    | `boolean`  | 是  | `false` |
| `-h, --help`          | 显示用法说明。                                                                                                                     | `boolean`  | 是  | `false` |

`targets`、`languages`、`autoImports`、`conditions` 和 `rootDirectory` 选项没有对应的标志，请通过 [`collectContext`](/docs/integrations/paraglide/reference/collect-context) 设置。

## 示例 [#example]

```bash
# 分析工作目录中的应用
npx paraglide-context

# 分析 monorepo 中的某个应用，并输出摘要
npx paraglide-context apps/web --verbose

# 分析 src 和一个共享软件包，并附带 5 行上下文
npx paraglide-context --include src --include ../shared --radius 5

# 声明一个别名并跳过打包工具配置
npx paraglide-context --alias ~=./src --no-bundler-config
```

## 其他说明 [#notes]

| 退出码 | 含义                                                     |
| --- | ------------------------------------------------------ |
| `0` | 运行完成 (无论是否有警告) 。                                       |
| `1` | 运行因错误而中止，例如无法写入元数据文件。此时会打印错误信息。                        |
| `2` | 存在未知标志、缺少值、指定了多个 `root`，或 `--radius` 不是非负整数。此时会打印用法说明。 |

* 警告以 `[paraglide-context]` 开头，输出到标准错误。
* 元数据文件仅在内容发生变化时才会写入，因此如果源内容没有变更，再次运行时不会写入任何文件。

## Sitemap

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