React Quickstart

Easily internationalize your React App with gt-react

Overview

This guide describes how to internationalize an existing React project.

We will cover 4 simple steps:

Running the setup wizard

Adding the GTProvider

Adding environment variables

Cleaning up strings


1. Running the setup wizard

The setup wizard will walk you through the process of internationalizing your project.

npx gtx-cli@latest init

To manually setup your project, follow the manual setup guide.

The setup wizard will:

  1. Install the required libraries.
  2. Configure your project's supported locales.
  3. Wrap your project's JSX components with the <T> component.
  4. Generate a production API key and project ID for your project.

See the setup wizard docs for more information.

2. Adding the GTProvider

Add the <GTProvider> component to your app. Spread the config JSON object into the config prop.

App.js
import { GTProvider } from "gt-react";
import MyApp from "./MyApp";
import config from "./gt.config.json";
 
export default function App() {
  return (
    <GTProvider {...config}> 
      <MyApp />
    </GTProvider> 
  );
}

For some React frameworks, the setup wizard will automatically add the GTProvider to your app. If this is the case, you can skip this step.

3. 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-react in development mode, you will need to add a development API key instead of a production one.

Navigate to the Dashboard.

  • Navigate to the Developer Keys page in the sidebar.

Click Create Dev API Key then copy the API key and Project ID to your clipboard.

Add the Project ID and API Key to your environment variables. Depending on your React framework, the environment variables may look slightly different. These instructions are for DEVELOPMENT ENVIRONMENTS ONLY.

VITE_GT_API_KEY="YOUR_GT_DEV_API_KEY"
VITE_GT_PROJECT_ID="YOUR_GT_PROJECT_ID"

Protect Your API Keys!

React environment variables are often bundled into your application at build time and are publicly visible in the client-side code. For security reasons, you should:

  • Only use development API keys during local development
  • Only use production API keys with the CLI tool for deployment
  • Never include any API keys as environment variables in production

4. 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:

Example.js
export default function Example() {
  const greeting = "Hello, world!";
  return <p>{greeting}</p>;
}

The setup wizard will not touch this string.

To fix this, you can use the useGT() hook to translate the string.

Example.js
import { useGT } from "gt-react";
export default function Example() {
  const t = useGT();
  return <p>{t("Hello, world!")}</p>;
}

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 React app in development mode.

npm run dev 

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 React API reference for detailed information about the <T> component and other available components.

Manual Setup

If you prefer to do the setup manually, follow the steps below.

1. Install libraries

Install the gt-react and gtx-cli libraries.

npm i gt-react
npm i gtx-cli --save-dev

2. Select languages

<GTProvider> is used to configure the behavior of gt-react. It should be placed as high up in your app as possible, ideally at the root.

Just pass a list of locale codes to add them to your app.

App.js
import { GTProvider } from "gt-react";
import MyApp from "./MyApp";
 
export default function App() {
  return (
    // French and Chinese support 
    <GTProvider locales={['fr', 'zh']}> 
      <MyApp />
    </GTProvider> 
  );
}

For a full list of <GTProvider> props and extended documentation, see the API reference.

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.

Example.js
import { T } from "gt-react";
 
export default function Example() {
  return (
    <T>
      <p>
        This gets translated.
      </p>
    </T>  
  );
}

Use the <Var> component to designate JSX content that should not be translated.

Example.js
import { T, Var } from "gt-react";
 
export default function Example() {
  return (
    <T>
      <p>
        This gets translated. <Var>This does not.</Var>
      </p>
    </T>  
  );
}

For strings, you can use useGT() for translation.

Example.js
import { useGT } from "gt-react";
 
export default function Example() {
  const t = useGT(); 
  return (
    <p>
      {t("This gets translated.")}
    </p>
  );
}

4. Add your environment variables

Add your API key and Project ID to your environment variables.

Navigate to the Dashboard.

  • Navigate to the Developer Keys page in the sidebar.

Click Create Dev API Key then copy the API key and Project ID to your clipboard.

Add the Project ID and API Key to your environment variables. Depending on your React framework, the environment variables may look slightly different. These instructions are for DEVELOPMENT ENVIRONMENTS ONLY.

VITE_GT_API_KEY="YOUR_GT_DEV_API_KEY"
VITE_GT_PROJECT_ID="YOUR_GT_PROJECT_ID"

Protect Your API Keys!

React environment variables are often bundled into your application at build time and are publicly visible in the client-side code. For security reasons, you should:

  • Only use development API keys during local development
  • Only use production API keys with the CLI tool for deployment
  • Never include any API keys as environment variables in production