getGT
API reference for the getGT() string translation function
Overview
The getGT function is an asynchronous function for translating strings at build time.
const t = await getGT();
<p>{ t('This text will be translated') }</p>;Build-time Translation:
getGT translations occur at build time, before your app is deployed.
Although you can pass variables to the translated string, you can only translate content known at build time.
Reference
Parameters
None
Returns
A promise for a callback function, t, which translates the supplied content.
Promise<(content: string, options?: InlineTranslationOptions) => string>| Name | Type | Description |
|---|---|---|
content | string | The string content to be translated. |
options? | InlineTranslationOptions | Translation options to customise the behaviour of t. |
Behaviour
Production
During the CD process, any content inside a t function will be translated before your application is deployed.
This ensures fast load times for all locales, but it can only translate content known at build time.
Once generated, translations are either (1) stored on the CDN or (2) stored in your app’s build output, according to your configuration. From there, the translated content is served to your users. If a translation is not found, it will fall back to the original content.
Make sure to follow the deployment guide here.
Development
During development, the t function will translate content on demand.
This is useful for prototyping how your app will look in different languages.
Remember to add a Dev API key to your environment to enable this behaviour.
You will see a delay for on‑demand translation in development.
This will not occur in production builds unless content is explicitly being translated on demand,
i.e., using tx or <Tx>.
Example
Basic usage
You can use getGT to translate strings.
import { getGT } from 'gt-next/server';
export default async function TranslateGreeting() {
const t = await getGT();
return (
<p>
{t('Hello, Alice!')}
</p>
);
}Note: "Alice" will be translated into the user's preferred language.
Using variables
You can pass variables to dictionary translations.
import { getGT } from 'gt-next/server';
export default async function TranslateGreeting() {
const t = await getGT();
return (
<p>
{t('Hello, {name}!', { name: 'Alice' })}
</p>
);
}Note: "Alice" will not be translated into the user's preferred language because it is a variable.
Using the ICU message format
gt-next supports the ICU message format, which lets you format your variables as well.
import { getGT } from 'gt-next/server';
export default async function TranslateGreeting() {
const t = await getGT();
return (
<p>
{t('There are {count, plural, =0 {no items} =1 {one item} other {{count} items}} in the basket', { count: 10 })}
</p>
);
}The ICU MessageFormat is a powerful way to format variables. For more information, see the ICU MessageFormat documentation.
Notes
- The
getGTfunction is a server-side function that translates strings. - Translation of strings with
getGToccurs before runtime, during the build process (unless in development).
Next steps
How is this guide?