This is an example project showcasing a create-react-app boilerplate app using gt-react.
This project was bootstrapped with Create React App.
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-react with React.
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/create-react-app
npm install-
(Optional) Create a
.envfile and populate it withREACT_APP_GT_PROJECT_IDandREACT_APP_GT_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 comprehensive list of steps done to reach this repo state:
npx create-react-app create-react-appnpm install gt-react gtx-clinpx gtx-cli setup --config ./src/gt.config.json && 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
- Import
gt.config.jsonand wrap your app in<GTProvider>in theindex.jsfile.
import { GTProvider } from 'gt-react';
import config from './gt.config.json'
...
<React.StrictMode>
<GTProvider {...config}>
<App />
</GTProvider>
</React.StrictMode>
...- (optional) Create a
.envfile and populate it withREACT_APP_GT_PROJECT_IDandREACT_APP_GT_API_KEY.- These environment variables are needed for local translations during development.
REACT_APP_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.
npm run dev- (optional) Add the [`<LocaleSelector]
To deploy this app to production:
- Add
GT_PROJECT_IDandGT_API_KEYto your.envfile- 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
loadTranslation.jsfile under./srcwith the following content:
export default async function loadTranslations(locale) {
const t = await import(`./locales/${locale}.json`);
return t.default;
}Add this to your <GTProvider/>
import { GTProvider } from 'gt-react';
import config from './gt.config.json'
import loadTranslations from './loadTranslations';
...
<React.StrictMode>
<GTProvider loadTranslations={loadTranslations} {...config}>
<App />
</GTProvider>
</React.StrictMode>
...- 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.