# gt: General Translation CLI tool: TypeScript
URL: https://generaltranslation.com/zh/docs/cli/formats/ts.mdx
---
title: TypeScript
description: 如何使用 General Translation 自动翻译 TypeScript 和 JavaScript 文件
---
## 概述
`gt` 可用于自动翻译项目中的 JavaScript (js) 和 TypeScript (ts) 文件。
原始文件中的所有语法和格式都会保留在翻译后的文件中。
我们将按以下 4 个步骤进行:
添加环境变量
安装 [`gt`](/docs/cli)
配置项目的 [`gt.config.json`](/docs/cli/reference/config) 文件
运行 [`gt translate`](/docs/cli/translate#translate)
***
## 第 1 步:添加环境变量
将生产环境的 API 密钥和项目 ID 添加到环境变量中。
这是使用 `gt` 工具所必需的。
你可以在 [General Translation 仪表板](https://generaltranslation.com/dashboard) 获取这些信息。
```bash title=".env"
GT_API_KEY=
GT_PROJECT_ID=
```
## 第 2 步:安装 `gt`
在项目中安装 `gt` 工具。
```bash
npm i gt
```
```bash
yarn add --dev gt
```
```bash
bun add --dev gt
```
```bash
pnpm add --save-dev gt
```
## 第 3 步:配置项目根目录下的 `gt.config.json` 文件
在项目根目录下创建一个 `gt.config.json` 文件,内容如下:
```json title="gt.config.json"
{
"defaultLocale": "en",
"locales": ["es", "fr"],
"files": {
"ts": {
"include": ["docs/[locale]/**/*.ts"]
}
}
}
```
将 `defaultLocale` 和 `locales` 改为与你的项目 locales 一致。
如果你的文件是 JavaScript 文件,可以使用 `js` 键而不是 `ts`。`include` 键中的字符串数组应包含可匹配所有 JS 文件的 glob 模式。
其中应使用 `[locale]` 占位符,以匹配文件的 locales。
有关 `gt.config.json` 文件的更多信息,请参阅 [配置](/docs/cli/reference/config) 文档。
## 第 4 步:将 `gt translate` 命令添加到你的构建流程中
在执行构建命令之前,将 `gt translate` 命令添加到你的构建或 CI 流程中,以便自动为你的项目添加翻译。
```json title="package.json"
{
"scripts": {
"translate": "npx gt translate",
"build": "npm run translate && "
}
}
```
这会为你的所有 locales 生成翻译,并保存到项目中。
如果你想把这些文件提交到代码仓库,也可以改为在提交前运行此命令。
完成!现在,每当项目发生变化时,它都会自动更新你的所有 TypeScript 文件。
***
## 注意事项
* 你可以使用 [`gt translate`](/docs/cli/translate) 命令,自动将翻译添加到项目中。
* 如果你想提交翻译文件,应在提交前先运行 `gt translate` 命令。
* 如需配置翻译文件的输出路径,请参阅[配置](/docs/cli/reference/config)文档。
## 后续步骤
* 请参阅[translate 命令](/docs/cli/translate),了解 CLI 的使用详情。