Translating Strings
How to translate strings
Overview
This guide is a step by step tutorial on how to translate strings in your Next.js app using
useGT()
, getGT()
, and tx()
.
Prerequisites
We assume that you already have installed gt-next
in your project and have followed or are currently following the Quick Start Guide.
Translating Strings
Client Side Components
For any client side strings, use useGT()
.
Remember that useGT()
must be called within a child component of <GTProvider>
.
Server Side Components
For any server side strings, use getGT()
.
Note:
When in development, if you are translating content at runtime, you will have to refresh the page to see the translated version of a string from getGT()
.
This does not happen in production.
Adding Variables
Variables are values that may change, but do not get translated. To add variables to your strings, use the following pattern:
This works with both useGT()
and getGT()
.
Dynamic Content
Say that you have a string that changes.
You can use the tx()
function to translate the string at runtime.
Notes
- For translating strings, use
useGT()
,getGT()
, andtx()
. useGT()
andgetGT()
translate before deployment, whereastx()
translates at runtime. Usetx()
sparingly.- Variables can be added to strings using the
{ variables: { key: value } }
pattern.
Next Steps
- Return to the Quick Start Guide to finish setting up your project for translation.
- See the API reference for
useGT()
,getGT()
, andtx()
.