Config

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.

next.config.mjs
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

PropDescription
defaultLocaleDefault locale for the application. English will be the fallback language when none is specified.
localesAn 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.
descriptionA natural‑language description of the site, used to aid translation.

Advanced Props

Prop

Type

PropDescription
projectIdProject ID, which can be included here or as an environment variable.
apiKeyAlthough not recommended, an API key, which can be included here. It can also be set as an environment variable.
devApiKeyAlthough not recommended, a development API key, which can be included here. It can also be set as an environment variable.
preferredModelProviderYour 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.
runtimeUrlBase URL for the GT API. To disable automatic translation, set this to an empty string.
cacheUrlURL where cached translations are stored. Can be customised to point to a custom cache server.
cacheExpiryTimeTime in milliseconds before locally cached translations expire.
renderSettingsAn object specifying loading behaviour for runtime translations.
maxConcurrentRequestsMaximum number of concurrent translation requests allowed to the GT API.
maxBatchSizeMaximum number of translations to batch together before sending a request.
batchIntervalInterval in milliseconds between batched translation requests. Helps control the rate at which requests are sent.
dictionaryOptional 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.
dynamicJsxCheckLogLevelControls validation of unwrapped dynamic content in translation components. Set to "error" to fail builds, "warn" to show warnings, or "off" to disable checking.
dynamicStringCheckLogLevelControls 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

PropDescription
methodThe method used to render the page. Options are skeleton, replace, and default.
timeoutThe 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-US and en-GB), behaves like replace. For locales with different languages (i.e. en-US and fr), 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.

gt.config.json
{
  "defaultLocale": "en-US",
  "locales": ["en-US", "es", "fr"],
}
next.config.mjs
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.

next.config.mjs
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

  • withGTConfig integrates GT translation functionality into your Next.js app and must be used in the root configuration file.
  • Parameters such as apiKey and projectId can be set directly in the configuration or as environment variables.
  • Advanced parameters such as renderSettings and _batchInterval allow fine-grained control over translation behaviour and performance.

Next steps

How is this guide?

withGTConfig