# 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]

* **先标准化区域设置，再进行验证。** 提供的每个区域设置代码 (`sourceLocale`、`targetLocale` 以及 `locales` 中的每一项) 都会先标准化为其规范的 BCP 47 形式，然后再进行验证。**存储的值是标准化后的形式**，而不是你传入的原始字符串。无效代码会使 `setConfig` 抛出错误。
* **自定义映射适用于 `sourceLocale`/`targetLocale`，但不适用于 `locales`。** `sourceLocale` 和 `targetLocale` 会结合 `customMapping` 进行验证，因此自定义别名可用于这两项。`locales` 中的每一项都会在**不使用**该映射的情况下进行验证，因此如果 `locales` 中出现 `customMapping` 的别名，就会被拒绝。
* **配置合并。** 新值会与传给构造函数的配置合并。不过，`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。        | `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` · **可选**

开发 API 密钥。在开发环境中，它的优先级高于 `apiKey`。

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

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

唯一的项目标识符。

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

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

翻译的默认源区域设置。该值会先标准化为其标准形式，并以该标准化后形式存储，然后再结合任何 `customMapping` **进行**验证 (因此这里也接受自定义别名) 。

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

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

翻译的默认目标区域设置。其值会先标准化为规范形式，并以该标准化后形式存储，然后再结合任何 `customMapping` 进行验证 (因此此处接受自定义别名) 。

### `locales` [#locales]

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

由受支持的区域设置代码组成的数组。每个代码都会被标准化为其规范形式，并以这种标准化后的形式存储，随后在**不应用** `customMapping` 的情况下进行验证——因此，如果 `locales` 中出现某个对 `sourceLocale`/`targetLocale` 有效的 `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]

* 配置更改会立即在后续的方法调用中生效。
* 每个区域设置代码在存储前都会先标准化为其规范形式 (存储的值为标准化后的形式) ，然后再进行验证。
* `sourceLocale` 和 `targetLocale` 会结合 `customMapping` 进行验证；`locales` 中的每个条目则不结合它进行验证。
* 调用 `setConfig` 时不会重新读取环境变量。
* 自定义映射会完全替换现有映射 (不会合并) 。
* 此更新**不是原子性的**——如果验证在中途失败，前面的一些属性可能已经设置完成。
* 该方法是同步的，返回 `void`。

## Sitemap

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