useGT
API reference for the `useGT` string translation function
Overview
The useGT function is a hook for translating strings at build time.
const t = useGT();
<p>{ t('This text will be translated') }</p>;Build-time translation:
useGT 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 callback function, t, that translates the provided content.
(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 isn’t 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 may notice a delay with on‑demand translation in development. This will not occur in production builds.
Example
Basic usage
You can use useGT to translate strings.
import { useGT } from 'gt-react';
export default function TranslateGreeting() {
const t = useGT();
return (
<p>
{t('Hello, Alice!')}
</p>
);
}Note: "Alice" will be translated into the user's preferred language.
Using variables
You can pass variables into dictionary translations.
import { useGT } from 'gt-react';
export default function TranslateGreeting() {
const t = useGT();
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 ICU message format
gt-react supports the ICU message format, which also lets you format your variables.
import { useGT } from 'gt-react';
export default function TranslateGreeting() {
const t = useGT();
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 details, see the ICU MessageFormat documentation.
Notes
- The
useGTfunction is a hook for translating strings. - The
useGThook can only be used within a component wrapped by a<GTProvider>component. - Translations using
useGTare resolved before runtime, during the build process (unless in development).
Next steps
- See
useTranslationsfor translating strings with a dictionary.
How is this guide?