<GTProvider>
Providing translations and locale information from server to client components
You should always use <GTProvider>
in a server component
In the Next.js App Router, components are server components by default.
This means they are executed on the server, and can perform server-specific actions like async
API calls and accessing environment variables.
Components which use client-side JavaScript, including React hooks like useState()
and useEffect()
, are client components. They must marked with 'use client'
or the descendants of another client component.
To use translations in client components, you must pass them from the server to the client using <GTProvider>
. <GTProvider>
uses React's Context API to give its descendants access to translations and locale information.
Inline translations
<T>
components which are the children of a <GTProvider>
must have an id
prop, or they will throw an error.
Hooks
The following hooks are available to the children of <GTProvider>
:
getLocale()
: Retrieves the user's current language as an ISO-639 language code.getDefaultLocale()
: Retrieves the application's default language as an ISO-639 language code.getGT()
: Used to initialize thet()
function, which loads a translation from the dictionary, if you are using a dictionary pattern.
Using <GTProvider>
with id
If your app is very large and you don't want to provide all of your translations in the server response, pass the optional id
prop to <GTProvider>
.
This will let you send a nested subset of your server-side dictionary to the client.
Every <T>
component which is a child of <GTProvider>
must have an id
prefixed with the id
you pass to <GTProvider>
.
Example
Using <GTProvider>
on the client side
It's better to use <GTProvider>
in a file not marked 'use client'
, so that it can fetch translations on the server-side.
If you really need to put <GTProvider>
on the client, import <GTProvider>
from 'gt-next/client'
(not recommended in server-first apps), and follow the gt-react
documentation.