Next.js Quickstart
Easily internationalize your Next.js App with gt-next
Overview
This guide describes how to internationalize an existing Next.js project that uses the App Router.
For a project using the Pages Router, follow the React docs.
We will cover 3 simple steps:
Running the setup wizard
Adding environment variables
Cleaning up strings
Are you using the Next.js pages router? Follow the React Quickstart guide instead.
Setup
1. Running the setup wizard
The setup wizard will walk you through the process of internationalizing your project.
To manually setup your project, follow the manual setup guide.
The setup wizard will:
- Install the required libraries.
- Configure your project's supported locales.
- Wrap your project's JSX components with the
<T>
component. - Add
withGTConfig()
to yournext.config.js
file. - Generate a production API key and project ID for your project.
See the setup wizard docs for more information.
2. Adding environment variables
The setup wizard will create a .env.local
file for you in the root of your project containing your production API key and project ID.
However, in order to use gt-next
in development mode, you will need to add a development API key instead of a production one.
Navigate to the Dashboard. Go to the Developer Keys page in the sidebar.
Click Create Dev API Key.
Add the Project ID and Development API key to your environment.
Protect Your API Keys!
Development keys should only ever be used in development. Likewise, production keys should only ever be used in production. Never commit your API keys to a public repository!
3. Cleaning up strings
The setup wizard will wrap all of your project's JSX components with the <T>
component.
However, you may notice that strings are unaffected.
For example, if you have a string constant like this:
The setup wizard will not touch this string.
To fix this, you can use the useGT()
hook and getGT()
function to translate the string.
Let's Try It Out!
Congratulations! 🥳 Your app is now multilingual! Let's see it in action.
See Your App in a Different Language
Add the <LocaleSelector>
component to your app.
This will allow you to select a different language for your app.
Tip: You can also change your language in your browser settings.
Start your Next.js app in development mode.
Open up your app in your preferred browser (usually at http://localhost:3000).
Troubleshooting
Shipping to Production
Follow our guide on shipping to production.
Next steps
- See our Next.js API reference for detailed information about the
<T>
component and other available components. - Improve your content with
<Num>
,<Currency>
,<Branch>
, and<Plural>
components. - Learn how to improve SEO with i18n routing (adding routes for each locale, e.g.
example.com/en
,example.com/fr
) and static site generation. - Mirror your app to support right-to-left languages such as Arabic and Hebrew.
Manual Setup
If you prefer to do the setup manually, follow the steps below.
1. Install libraries
Install the gt-next and gtx-cli libraries.
2. Add the withGTConfig()
plugin
Add withGTConfig()
to your next.config.js
file.
You can specify the languages you want to support by passing an array of locale codes.
3. Add the <T>
component
Wrap any nested JSX content in the <T>
component to make it translatable.
For more information, check out the guide on using <T>
components.
Use the <Var>
component to designate JSX content that should not be translated.
For strings, you can use useGT()
or getGT()
for translation.
4. Add your environment variables
Add your API key and Project ID to your local environment.
Navigate to the Dashboard. Go to the Developer Keys page in the sidebar.
Click Create Dev API Key.
Add the Project ID and Development API key to your environment.
Protect Your API Keys!
Development keys should only ever be used in development. Likewise, production keys should only ever be used in production. Never commit your API keys to a public repository!