# generaltranslation: General Translation Core SDK: setConfig URL: https://generaltranslation.com/zh/docs/core/class/set-config.mdx --- title: setConfig description: GT setConfig 方法的 API 参考文档 --- ## 概述 `setConfig` 方法用于更新现有 GT 实例的配置。 这样,你可以在实例创建后修改 API 凭据、区域设置及其他配置。 ```typescript const gt = new GT(); gt.setConfig({ apiKey: 'your-new-api-key', projectId: 'your-project-id', sourceLocale: 'en', targetLocale: 'es' }); ``` `setConfig` 方法会验证所有提供的区域设置代码,并将其与传递给构造函数的现有配置合并。 *** ## 参考 ### 参数 `GTConstructorParams` 对象支持与构造函数相同的属性: | 属性 | 类型 | 可选 | 描述 | | --------------- | --------------- | -- | ---------------- | | `apiKey` | `string` | ✓ | 翻译服务的生产环境 API 密钥 | | `devApiKey` | `string` | ✓ | 开发环境 API 密钥 | | `projectId` | `string` | ✓ | 项目的唯一标识符 | | `sourceLocale` | `string` | ✓ | 翻译的默认源区域设置 | | `targetLocale` | `string` | ✓ | 翻译的默认目标区域设置 | | `locales` | `string[]` | ✓ | 支持的区域设置代码数组 | | `baseUrl` | `string` | ✓ | 自定义 API 基础 URL | | `customMapping` | `CustomMapping` | ✓ | 自定义区域设置代码映射 | ### 返回值 `void` - 此方法会直接在实例上更新配置。 *** ## 示例 在本示例中,我们将目标区域设置从西班牙语切换为法语。 ```typescript const gt = new GT({ sourceLocale: 'en', targetLocale: 'es' }); // 切换到法语 gt.setConfig({ targetLocale: 'fr' }); ``` *** ## 说明 * 配置更改会立即在后续方法调用中生效 * 调用 `setConfig` 时不会重新读取环境变量 * 自定义映射会完全替换现有映射 (不会合并) * 此更新**不是原子性的** — 如果在中途验证失败 (例如,在第二个区域设置处) ,前面的属性可能已经被设置 * 该方法是同步方法,返回 `void` ## 后续步骤 * **[使用构造函数进行初始化](/docs/core/class/constructor)** * **[开始翻译内容](/docs/core/class/methods/translation/translate)** * **[了解 GTConstructorParams](/docs/core/types/gt-constructor-params)**