GTProvider
API reference for the <GTProvider> component
Overview
The <GTProvider> component provides General Translation (GT) context to its children, enabling them to access translated content.
It is required for any client-side translations in your application.
When to Use
- Wrap your entire application in
<GTProvider>to enable client-side translations. - When working with dictionaries, you can optionally specify an
idto limit the dictionary data sent to the client, optimising performance for large dictionaries. - The
<GTProvider>component is used for both inline<T>and dictionaries.
Reference
Props
Prop
Type
Description
| Prop | Description |
|---|---|
children | Any component, or the parent of any component, that needs to translate or access translation information on the client side. This should include any components using <T>, useGT, or other translation utilities. |
projectId? | The project ID required for General Translation cloud services. |
id? | The ID of a nested dictionary to limit the data sent to the client. This is useful for large projects with extensive dictionaries. |
dictionary? | The translation dictionary for the project. |
locales? | The list of approved locales for the project. |
defaultLocale? | The default locale to use if no other locale is found. |
locale? | The current locale, if already set. |
cacheUrl? | The URL of the cache service for fetching translations. |
runtimeUrl? | The URL of the runtime service for fetching translations. |
renderSettings? | The settings for rendering translations. |
_versionId? | The version ID for fetching translations. |
devApiKey? | The API key for development environments. |
metadata? | Additional metadata to pass to the context. |
Returns
React.JSX.Element|undefined containing the children passed to this component.
Examples
Basic usage
Wrap your application in <GTProvider> to add translations to your app.
Don’t forget to add a list of supported locales to enable translation.
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import App from "./App.tsx";
import { GTProvider } from "gt-react";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<GTProvider locales={['es', 'fr']}> // Enable Spanish and French // [!code highlight]
<App />
</GTProvider> // [!code highlight]
</StrictMode>
);Dictionaries
You can also pass a dictionary to the <GTProvider> component and then access it with the useTranslations hook.
import dictionary from "./dictionary.js";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<GTProvider locales={['es', 'fr']} dictionary={dictionary}> // [!code highlight]
<App />
</GTProvider>
</StrictMode>
);For more information on using dictionaries, see this guide.
Adding configuration
If you’re not keen on passing props directly to the <GTProvider> component, you can create a configuration file and pass that to the component.
It also integrates directly with the gtx-cli translate command, so you don’t have to specify things like locales manually as a parameter.
{
"projectId": "your-project-id",
"locales": ["es", "fr"],
"defaultLocale": "en-GB",
"_versionId": "translation-version-id" // allows for rolling back to previous translations (autogenerated by the CLI)
}All you need to do is pass this to the <GTProvider> component.
import config from "../gt.config.json";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<GTProvider {...config}> // [!code highlight]
<App />
</GTProvider>
</StrictMode>
);Custom translation loader
You can use the loadTranslations prop to load translations from a custom source.
This is useful when you need to load translations from a different source, such as a custom API.
import loadTranslations from "./loadTranslations";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<GTProvider locales={['es', 'fr']} loadTranslations={loadTranslations}> // [!code highlight]
<App />
</GTProvider>
</StrictMode>
);Render settings
Render settings control the loading behaviour for translations.
There are two fields: timeout and method.
timeoutis the number of milliseconds to wait for a translation to load before showing a fallback (default:8000ms).methodis the method used to load translations ("skeleton","replace", or"default").
<GTProvider renderSettings={{ method: "skeleton", timeout: 1000 }}>
<App />
</GTProvider>Each render setting dictates different loading behaviour:
"skeleton" returns null until the translations are loaded.
"replace" returns the fallback content until the translations are loaded.
"default" returns null until the translations are loaded, unless the fallback locale uses the same language as the current locale (i.e., en-US and en-GB).
In that case, it returns the fallback content immediately until the translations are loaded.
Notes
- The
<GTProvider>must wrap all<T>components and other translation‑related functions. Learn more here.
Next steps
- Learn more about the
<T>component for translating text and components.
How is this guide?