This is an example project showcasing a multilingual Next.js App using gt-next.
Change your browser language to see the translations in action.
One-click deploy to Vercel:
Everything works out of the box!
See the docs for more information on how to use gt-next with Next.js.
To clone this example and see it in action yourself, follow the following steps:
- Run
git clone https://github.com/generaltranslation/gt.git
cd gt-libraries/examples/next-create-app
npm install-
(Optional) Create a
.local.envfile and populate it withGT_PROJECT_IDandGT_API_KEY, obtainable via the GT Dashboard here- This example comes with translations for French, Spanish, and Chinese out of the box, but if you want to experiment with other locales or modify some content, you'll need to add your own API keys.
-
Run
npm run dev
Here is a list of steps done to reach this repo state:
npx create-next-app@latestnpm install gt-next gtx-clinpx gtx-cli setup && npx-gtx-cli init- Setup will automatically add the
<T>components to your app. - When calling
initspecify "remote" as the location of your language files and en, zh, and fr as your locales.
- Setup will automatically add the
- Add locales to the
next.config.tsfile:
export default withGTConfig(nextConfig, {
defaultLocale: 'en-US',
locales: ['en-US', 'fr', 'es', 'zh'],
});- (optional) Create a
.local.envfile and populate it withGT_PROJECT_IDandGT_API_KEY.- These environment variables are needed for local translations during development. The
GT_API_KEYshould be a development API key. A separate production API Key is needed for subsequent steps when deploying to production.
- These environment variables are needed for local translations during development. The
npm run dev
To deploy this app to production:
- Add
GT_PROJECT_IDandGT_API_KEYto your.env.localfile- The
GT_API_KEYshould be a production API key.
- The
- Add
npx gtx-cli translateto your build step before the build command. - Deploy to Vercel / Render / etc..
This repo is setup to use local translations in production.
If you are following the step-by-step guide, you will also need to follow these steps to use local translations. If you omit these steps, your production app will use the GT CDN for translation files.
- Add a
loadTranslations.tsfile under./srcwith the following content:
export default async function loadTranslations(locale: string) {
const t = await import(`../public/_gt/${locale}.json`);
return t.default;
}- Instead of running the command in Step 2 above, run:
- When calling
initspecify "local" as the location of your language files and en, zh, and fr as your locales
npx gtx-cli translateFor more information on local translation, check out our guide on local translation.