withGTConfig
API reference for withGTConfig(), formerly initGT()
Overview
withGTConfig is the primary way to configure the gt-next library.
It directly wraps a NextConfig object.
import { withGTConfig } from 'gt-next/config';
const nextConfig = {
// your existing next.config.js
}
export default withGTConfig(nextConfig, {
// Additional configuration options
});Legacy
initGT is the legacy way to configure the gt-next library. It returns a callback function which is then invoked on the NextConfig object.
The props for both functions are the same, except that withGTProps also requires NextConfig to be passed in.
Use withGTConfig to:
- Configure supported languages and the default locale (a.k.a. fallback language).
- Set API keys and project IDs for accessing GT services.
- Set loading behaviour.
- Configure timeout settings.
- Set up custom endpoints.
- Customise translation behaviour, caching, and request batching.
- Configure build-time validation via the SWC plugin.
withGTConfig must be used in your next.config.js file to enable translation functionality.
Reference
By default, withGTConfig will look for a gt.config.json file in the same directory as your next.config.js file.
This JSON file will be loaded and merged with the configuration passed to withGTConfig.
See the gt.config.json reference for more information on the configuration file.
The CLI tool will only read the configuration from the gt.config.json file, so
we recommend using the gt.config.json file as the single source of truth for your app.
Additional configuration options not in the gt.config.json file can be passed to withGTConfig directly as props.
Required props
Prop
Type
Recommended Props
Prop
Type
| Prop | Description |
|---|---|
defaultLocale | Default locale for the application. English will be the fallback language when none is specified. |
locales | An explicit list of supported locales for the application. If a non‑supported request is received, it will reroute to the browser’s next‑preferred language in the list. Will fall back to defaultLocale if no matches can be found. |
description | A natural‑language description of the site, used to aid translation. |
Advanced Props
Prop
Type
| Prop | Description |
|---|---|
projectId | Project ID, which can be included here or as an environment variable. |
apiKey | Although not recommended, an API key, which can be included here. It can also be set as an environment variable. |
devApiKey | Although not recommended, a development API key, which can be included here. It can also be set as an environment variable. |
preferredModelProvider | Your preferred AI model provider. Currently only Anthropic or OpenAI are enabled. Leave this blank and we’ll determine the best provider on a translation-by-translation basis. During periods of high usage or when a provider is disabled, we can’t guarantee that your preferred provider will be used. |
runtimeUrl | Base URL for the GT API. To disable automatic translation, set this to an empty string. |
cacheUrl | URL where cached translations are stored. Can be customised to point to a custom cache server. |
cacheExpiryTime | Time in milliseconds before locally cached translations expire. |
renderSettings | An object specifying loading behaviour for runtime translations. |
maxConcurrentRequests | Maximum number of concurrent translation requests allowed to the GT API. |
maxBatchSize | Maximum number of translations to batch together before sending a request. |
batchInterval | Interval in milliseconds between batched translation requests. Helps control the rate at which requests are sent. |
dictionary | Optional configuration file path for the dictionary. Similar to i18n, it accepts a string to specify a custom path. Dictionaries named dictionary.js (or .jsx, .ts, .tsx, etc.) and placed at the root or in the src folder are supported by default. |
dynamicJsxCheckLogLevel | Controls validation of unwrapped dynamic content in translation components. Set to "error" to fail builds, "warn" to show warnings, or "off" to disable checking. |
dynamicStringCheckLogLevel | Controls validation of translation function arguments to ensure string literals are used. Set to "error" to fail builds, "warn" to show warnings, or "off" to disable checking. |
Returns
A function (NextConfig) => NextConfig that augments the Next.js configuration object with the specified GT settings.
Exceptions
Throws an Error if the projectId is missing and default URLs are used, or if the API key is required but missing.
Render settings
Render settings control the behaviour of translations while they load. This only applies to translations that occur at runtime. If the translation is cached, the response time is too low to warrant loading behaviour.
Prop
Type
| Prop | Description |
|---|---|
method | The method used to render the page. Options are skeleton, replace, and default. |
timeout | The time in milliseconds before the method times out. Default is 8000 ms. |
Render methods
skeleton: Renders a fragment.replace: Renders content in the default language while waiting.default: For locales with the same language (i.e.en-USanden-GB), behaves like replace. For locales with different languages (i.e.en-USandfr), behaves like skeleton.
Timeout
Timeouts apply only to runtime translations, i.e. translations performed on demand because they haven’t been cached.
By default, timeouts are set to 8 seconds. This design choice is to accommodate Vercel users, who have a default 10‑second timeout for serverless functions on the free plan.
Examples
Render settings
This example configures gt-next to render a skeleton while translations are loading.
If loading takes longer than 8 seconds, the method times out and renders the default-language content.
{
"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,
},
});Build-time validation
This example configures the SWC plugin to treat dynamic content violations as build errors rather than warnings.
import { withGTConfig } from 'gt-next/config';
const nextConfig = {
// Your other Next.js configurations
};
export default withGTConfig(nextConfig, {
// Fail builds on dynamic JSX violations
dynamicJsxCheckLogLevel: 'error',
// Warn on dynamic string violations
dynamicStringCheckLogLevel: 'warn',
});Notes
withGTConfigintegrates GT translation functionality into your Next.js app and must be used in the root configuration file.- Parameters such as
apiKeyandprojectIdcan be set directly in the configuration or as environment variables. - Advanced parameters such as
renderSettingsand_batchIntervalallow fine-grained control over translation behaviour and performance.
Next steps
How is this guide?