# General Translation Platform: setConfig
URL: https://generaltranslation.com/zh/docs/platform/core/reference/gt-class/set-config.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 更新现有 GT 实例的配置。setConfig 的 API 参考。

在 [GT](/docs/platform/core/reference/gt-class/constructor) 实例创建后，无需重新构造新实例即可更改其 API 凭据、区域设置及其他设置。

## 概览 [#overview]

调用 `setConfig` 可原地修改实例。新值会与传递给构造函数的配置合并，并应用到之后的每次方法调用中。

```typescript
const gt = new GT();

gt.setConfig({
  apiKey: 'your-new-api-key',
  projectId: 'your-project-id',
  sourceLocale: 'en',
  targetLocale: 'es',
});
```

签名：

```typescript
setConfig(params: GTConstructorParams): void
```

*注意：`setConfig` 是同步函数，返回 `void`；它会原地更新该实例。*

## 工作方式 [#how-it-works]

* **保留配置的标识。** 区域设置的写法会原样保留。所有提供的区域设置字段 (包括 `locales`) 都会根据当前生效的自定义映射进行验证。无效代码会抛出错误。返回的区域设置代码遵循[构造函数规则](/docs/platform/core/reference/gt-class/constructor#how-it-works)。
* **更改凭据。** 如需切换凭据或项目设置，请使用此方法。不支持通过直接为属性赋值来进行这些更改。
* **配置合并。** 新值会与传给构造函数的配置合并。不过，`customMapping` 不会参与合并，而是会完全替换任何现有映射。
* **不会重新读取环境变量。** 调用 `setConfig` 时，不会重新读取环境变量 (`GT_API_KEY`、`GT_DEV_API_KEY`、`GT_PROJECT_ID`) 。
* **更新失败。** 如果更新失败，部分设置可能已被更改。

## 参数 [#parameters]

`setConfig` 接受一个必填的 [`GTConstructorParams`](/docs/platform/core/reference/types/gt-constructor-params) 对象，其属性与 [constructor](/docs/platform/core/reference/gt-class/constructor) 相同：

| 参数                                 | 描述                              | 类型                                                                    | 可选 | 默认值 |
| ---------------------------------- | ------------------------------- | --------------------------------------------------------------------- | -- | --- |
| [`apiKey`](#api-key)               | 翻译服务的项目 API Key。                | `string`                                                              | 是  | —   |
| [`devApiKey`](#dev-api-key)        | 备用项目 API Key，在未设置 `apiKey` 时使用。 | `string`                                                              | 是  | —   |
| [`projectId`](#project-id)         | 唯一的项目标识符。                       | `string`                                                              | 是  | —   |
| [`sourceLocale`](#source-locale)   | 翻译的默认源区域设置。                     | `string`                                                              | 是  | —   |
| [`targetLocale`](#target-locale)   | 翻译的默认目标区域设置。                    | `string`                                                              | 是  | —   |
| [`locales`](#locales)              | 支持的区域设置代码。                      | `string[]`                                                            | 是  | —   |
| [`baseUrl`](#base-url)             | 自定义 API 基础 URL。                 | `string`                                                              | 是  | —   |
| [`customMapping`](#custom-mapping) | 自定义区域设置代码映射和属性覆盖。               | [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) | 是  | —   |

`params` 对象本身是必填的，但其中的每个属性都是可选的——只需传入你想更改的设置即可。

### `apiKey` [#api-key]

**类型** `string` · **可选**

翻译服务的项目 API Key。

### `devApiKey` [#dev-api-key]

**类型** `string` · **可选**

`apiKey` 的兼容性别名，已弃用。当 `apiKey` 未设置时使用，与运行环境无关。

### `projectId` [#project-id]

**类型** `string` · **可选**

唯一的项目标识符。

### `sourceLocale` [#source-locale]

**类型** `string` · **可选**

翻译的默认源区域设置。其拼写会原样保留，并结合当前生效的 `customMapping` (包括别名) 进行验证。

### `targetLocale` [#target-locale]

**类型** `string` · **可选**

翻译的默认目标区域设置。其拼写会保持不变，并结合当前生效的 `customMapping` (包括别名) 进行验证。

### `locales` [#locales]

**类型** `string[]` · **可选**

由受支持的区域设置标识组成的数组。每个代码都会保持原样，并使用生效的 `customMapping` 进行验证；也接受自定义别名。

### `baseUrl` [#base-url]

**类型** `string` · **可选**

自定义 API 基础 URL。

### `customMapping` [#custom-mapping]

**类型** [`CustomMapping`](/docs/platform/core/reference/types/custom-mapping) · **可选**

自定义区域设置代码映射及属性覆盖。与其他设置不同，新的 `customMapping` 会完全替换现有映射，而不是与其合并。

## 返回值 [#returns]

**类型** `void`

此方法会原地更新实例配置，不会返回任何值。

## 示例 [#examples]

```typescript
const gt = new GT({
  sourceLocale: 'en',
  targetLocale: 'es',
});

// 将目标区域设置从西班牙语切换为法语
gt.setConfig({
  targetLocale: 'fr',
});
```

## 说明 [#notes]

* 配置更改会立即在后续的方法调用中生效。
* 已配置的区域设置标识会原样保留，并结合当前生效的自定义映射进行验证 (包括 `locales` 中的条目) 。
* 空字符串或 `undefined` 不会清除凭据。
* 调用 `setConfig` 时不会重新读取环境变量。
* 自定义映射会完全替换现有映射 (不会合并) 。
* 如果更新失败，部分设置可能已被更改。
* 该方法是同步的，返回 `void`。

## Sitemap

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