withGTConfig()
withGTConfig() 的 API 参考,前身为 initGT()
概述
withGTConfig()
是配置 gt-next
库的主要方式。
它直接包装了一个 NextConfig
对象。
import { withGTConfig } from 'gt-next/config';
const nextConfig = {
// your existing next.config.js
}
export default withGTConfig(nextConfig, {
// Additional configuration options
});
旧版
initGT()
是配置 gt-next
库的旧方法。它返回一个函数回调,然后在 NextConfig
对象上调用。
这两个函数的参数是相同的,唯一的区别是 withGTProps
需要额外传入 NextConfig
。
使用 withGTConfig()
可以:
- 配置支持的语言和默认语言(即回退语言)。
- 设置用于访问 GT 服务的 API 密钥和项目 ID。
- 设置加载行为。
- 配置超时设置。
- 设置自定义端点。
- 自定义翻译行为、缓存和请求批处理。
必须在你的 next.config.js
文件中使用 withGTConfig()
,以启用翻译功能。
参考
默认情况下,withGTConfig()
会在与您的 next.config.js
文件相同的目录中查找 gt.config.json
文件。
此 json 文件将被加载并与传递给 withGTConfig()
的配置合并。
有关配置文件的更多信息,请参阅 gt.config.json 参考。
CLI 工具只会从 gt.config.json
文件中读取配置,因此
我们建议使用 gt.config.json
文件作为您应用程序的真实配置来源。
不在 gt.config.json
文件中的其他配置选项可以直接作为 props 传递给 withGTConfig()
。
必需的 Props
Prop | Type | Default |
---|---|---|
nextConfig? | NextConfig | - |
推荐的 Props
Prop | Type | Default |
---|---|---|
description?? | string | undefined |
locales?? | string[] | undefined |
defaultLocale?? | string | locales[0] || 'en' |
Prop | 描述 |
---|---|
defaultLocale | 应用程序的默认语言环境。当未指定语言环境时,英语将作为后备语言。 |
locales | 应用程序支持的语言环境的独占列表。如果收到不受支持的请求,将重新路由到列表中浏览器的下一个首选语言。如果找不到匹配项,将回退到 defaultLocale 。 |
description | 网站的自然语言描述,用于辅助翻译。 |
高级 Props
Prop | Type | Default |
---|---|---|
dictionary?? | string | - |
maxBatchSize?? | number | 25 |
batchInterval?? | number | 50 |
maxConcurrentRequests?? | number | 100 |
renderSettings?? | RenderSettings | - |
cacheExpiryTime?? | number | 60000 |
cacheUrl?? | string | - |
runtimeUrl?? | string | - |
preferredModelProvider?? | "anthropic" | "openai" | - |
devApiKey?? | string | - |
apiKey?? | string | - |
projectId?? | string | - |
Prop | Description |
---|---|
projectId | 项目 ID,可以在此处包含或作为环境变量。 |
apiKey | 虽然不推荐,但可以在此处包含 API 密钥。也可以作为环境变量包含。 |
devApiKey | 虽然不推荐,但可以在此处包含开发 API 密钥。也可以作为环境变量包含。 |
preferredModelProvider | 您的首选 AI 模型提供商。目前仅启用 Anthropic 或 OpenAI。将此项留空,我们将根据每次翻译确定最佳提供商。在使用高峰期或提供商被禁用时,我们无法保证会使用您的首选提供商。 |
runtimeUrl | GT API 的基础 URL。要禁用自动翻译,请将此项设置为空字符串。 |
cacheUrl | 存储缓存翻译的 URL。可以自定义以指向自定义缓存服务器。 |
cacheExpiryTime | 本地缓存翻译过期前的时间(以毫秒为单位)。 |
renderSettings | 指定运行时翻译加载行为的对象。 |
maxConcurrentRequests | 允许向 GT API 发送的最大并发翻译请求数。 |
maxBatchSize | 在发送请求之前批量处理的最大翻译数量。 |
batchInterval | 批量翻译请求之间的间隔(以毫秒为单位)。有助于控制发送请求的速率。 |
dictionary | 字典的可选配置文件路径。与 i18n 类似,它接受字符串来指定自定义路径。默认支持名为 dictionary.js (或 .jsx 、.ts 、.tsx 等)并放置在根目录或 src 文件夹中的字典。 |
返回值
一个函数 (NextConfig) => NextConfig
,使用指定的 GT 设置增强 Next.js 配置对象。
异常
如果缺少 projectId
且使用默认 URL,或者需要 API 密钥但缺失时,会抛出 Error
。
渲染设置
渲染设置控制翻译在加载时的行为。 这仅适用于运行时进行的翻译。 如果翻译已被缓存,响应时间非常短,无需特殊加载行为。
Prop | Type | Default |
---|---|---|
timout? | number | 8000 |
method? | "skeleton" | "replace" | "default" | default |
Prop | Description |
---|---|
method | 用于渲染页面的方法。可选项有 skeleton 、replace 和 default 。 |
timeout | 方法超时前的毫秒数。默认值为 8000 毫秒。 |
渲染方法
skeleton
:渲染一个片段。replace
:在等待时以默认语言渲染内容。default
:对于相同语言的地区(如en-US
和en-GB
),行为与 replace 相同。对于不同语言的地区(如en-US
和fr
),行为与 skeleton 相同。
超时
超时仅适用于运行时翻译,或需要按需执行且尚未缓存的翻译。
超时时间默认设置为 8 秒。 此设计决策是为了方便 vercel 用户,因为其免费计划下 serverless 函数的默认超时时间为 10 秒。
示例
渲染设置
此示例配置 gt-next
在等待翻译加载时渲染骨架屏。
如果翻译耗时超过 8 秒,该方法将超时并渲染默认语言内容。
{
"defaultLocale": "en-US",
"locales": ["en-US", "es", "fr"],
}
import { withGTConfig } from 'gt-next/config';
const nextConfig = {
// Your other next.js configurations
};
export default withGTConfig(nextConfig, {
renderSettings: {
method: 'skeleton',
timeout: 10000,
},
});
注意事项
withGTConfig()
将 GT 翻译功能集成到你的 Next.js 应用中,必须在根配置文件中使用。- 类似
apiKey
和projectId
这样的参数可以直接在配置中设置,也可以作为环境变量设置。 - 高级参数如
renderSettings
和_batchInterval
允许对翻译行为和性能进行精细控制。
下一步
这份指南怎么样?