General Translation  
Next.jsConfig

initGT()

API Reference for the initGT() method.

Overview

initGT() allows you to customize the behavior of the gt-next library. It is not required to use initGT() to enable translation functionality, but it is recommended to configure the library to suit your needs.

Use initGT() to:

  • Configure supported languages and default locale (a.k.a fallback language).
  • Set API keys and project IDs for accessing GT services.
  • Set loading behavior.
  • Configure timeout settings.
  • Set up custom endpoints.
  • Customize translation behavior, caching, and request batching.

initGT() must be used in your next.config.js file to enable translation functionality.

Reference

Recommended Props

PropTypeDefault
defaultLocale?
string
locales[0] || 'en'
locales?
string[]
undefined
description?
string
undefined
PropDescription
defaultLocaleDefault locale for the application. English will be the fallback language when none is specified.
localesAn exclusive list of supported locales for the application. If a non-supported request is received will reroute to the browser's next-preferred language in the list. Will fallback to defaultLocale if no matches can be found.
descriptionA natural language description of the site, used to aid translation.

Advanced Props

PropTypeDefault
projectId?
string
-
apiKey?
string
-
devApiKey?
string
-
preferredModelProvider?
"anthropic" | "openai"
-
runtimeUrl?
string
-
cacheUrl?
string
-
cacheExpiryTime?
number
60000
renderSettings?
RenderSettings
-
maxConcurrentRequests?
number
100
batchInterval?
number
50
maxBatchSize?
number
25
i18n?
string
-
dictionary?
string
-
PropDescription
projectIdProject ID, which can be included here or as an environment variable.
apiKeyAPI key, which can be included here or as an environment variable.
devApiKeyDevelopment API key, which can be included here or as an environment variable.
preferredModelProviderYour first choice AI model provider. Currently only Anthropic or OpenAI are enabled. Leave this blank and we'll figure out the best provider on a translation-by-translation basis. In 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 customized to point to a custom cache server.
cacheExpiryTimeTime in milliseconds before locally cached translations expire.
renderSettingsAn object specifying loading behavior 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.
i18nOptional configuration filepath for custom getLocale() functions. If provided as a string, it will be resolved as a path. Otherwise, defaults are used (recommended).
dictionaryOptional configuration filepath for the dictionary. Similar to i18n, it accepts a string to specify a custom path. Dictionaries called dictionary.js (or .jsx, .ts, .tsx etc.) and placed at the root or in the src folder are supported by default.

Returns

A function (NextConfig) => NextConfig that enhances the Next.js configuration object with the specified GT settings.

Exceptions

Throws and Error if the projectId is missing and default URLs are used, or if the API key is required and missing.


Render settings

Render settings controls the behavior of translations while they are loading. This only applies to translations that are happening at runtime. If the translation is cached, response time is too low to justify loading behavior.

PropTypeDefault
method
"skeleton" | "replace" | "default"
default
timout
number
8000
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 default language while waiting.
  • default: For locales with the same language (ie en-US and en-GB) behaves like replace. For locales with different languages (ie en-US and fr), behaves like skeleton.

Timeout

Timeouts only apply to runtime translations, or translations that need to be performed on demand as they have not been cached.

Timeouts are set to 8 seconds by default. This design decision is to facilitate vercel users who have a default 10-second timeout for serverless functions on the free plan.


Examples

Supported locales

This example configures gt-next with English (en-US) as the default locale. It exclusively supports translations in Spanish (es) and French (fr), and provides a description for context-aware translation.

The site will fallback to the best suited language if none of the locales match. It will look for matching languages (i.e., en-US and en-GB), as well as the other preferred languages the user has set in their browser. If all else fails, then it will fallback to the default language.

If you want to support all languages, leave locales blank. Additionally, locales can be configured on the GT dashboard.

next.config.mjs
import { initGT } from 'gt-next/config';
 
const withGT = initGT({
    defaultLocale: 'en-US',
    locales: ['en-US', 'es', 'fr'],
    description: 'A personal blog about technology and travel',
});
 
export default withGT({});

Render settings

This example configures gt-next to render a skeleton while waiting for translations to load. If the translation takes longer than 8 seconds, the method will time out and render the default language content.

next.config.mjs
import { initGT } from 'gt-next/config';
 
// locales field is left empty, so this configuration supports all 100+ languages
const withGT = initGT({
    defaultLocale: 'en-US',
    renderSettings: {
        method: 'skeleton',
        timeout: 10000,
    },
});
 
export default withGT({});

Notes

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

Next Steps

On this page