Installation (React)
Check our React example application
To install Tolgee React integration library run:
- npm
- yarn
- pnpm
npm install @tolgee/react
yarn add @tolgee/react
pnpm install @tolgee/react
Init Tolgee and wrap your application in TolgeeProvider
.
import { Tolgee, DevTools, TolgeeProvider, FormatSimple } from "@tolgee/react";
const tolgee = Tolgee()
.use(DevTools())
.use(FormatSimple())
.init({
language: 'en',
// for development
apiUrl: process.env.REACT_APP_TOLGEE_API_URL,
apiKey: process.env.REACT_APP_TOLGEE_API_KEY,
// for production
staticData: {
...
}
});
...
<TolgeeProvider
tolgee={tolgee}
fallback="Loading..." // loading fallback
>
<your-app-components>
</TolgeeProvider>
Check all tolgee options and tolgee plugins.
If you bootstrapped your application with Create React App, your .env.development.local
file should look like this:
REACT_APP_TOLGEE_API_URL=https://app.tolgee.io
REACT_APP_TOLGEE_API_KEY=tgpak_gfpwiojtnrztqmtbna3dczjxny2ha3dmnu4tk4tnnjvgc
Never leak your API key! You don't want visitors of your site to edit your translations. That's why we recommend to use .env files to store your API key. API key is then omitted for production build.
Otherwise, you can set the properties directly, or you can use plugins like dotenv-webpack plugin.
Obtaining Tolgee API key is described in Integration chapter.
Preparing for production
Tolgee will automatically omit DevTools
from your bundle when you build your app for production. So it won't fetch translations directly from Tolgee Platform and it won't allow users to modify translations.
There are generally two ways how to use translations in production:
- Tolgee Content Delivery - translations are loaded dynamically from fast and reliable storage
- Providing localization files directly - your translations are bundled with your code
In production mode, you should never use localization data directly from Tolgee REST API, because it can negatively affect your page performance.