Installation
To use Tolgee in your React application, you need to follow these steps:
- Install the Tolgee integration library
- Set up the environment variables
- Configure the TolgeeProvider
- Preparing for production
Install the Tolgee integration library
To install Tolgee Svelte integration library, run the following command.
- npm
- yarn
- pnpm
npm install @tolgee/svelte
yarn add @tolgee/svelte
pnpm install @tolgee/svelte
Set up the environment variables
Once the library is installed, you need to initialize it. For initialization, you need the Tolgee API URL, and the Tolgee API Key. To generate the API Key, follow the step-by-step instructions mentioned on the API keys and PAT tokens page.
Make sure you don't leak your API key. If the API key is leaked, visitors can edit the translations on your site.
After generating the API key, add these credentials. If you bootstrapped the application with SvelteKit, add them to the .env.development.local
file. Your .env.development.local
should look as follow.
VITE_TOLGEE_API_KEY=tgpak_gfpwiojtnrztqmtbna3dczjxny2ha3dmnu4tk4tnnjvgc
VITE_TOLGEE_API_URL=https://app.tolgee.io
Configure the TolgeeProvider
Next, initialize Tolgee and wrap the application in TolgeeProvider
.
<script>
import { TolgeeProvider, Tolgee, DevTools, FormatSimple } from '@tolgee/svelte';
const tolgee = Tolgee()
.use(DevTools())
.use(FormatSimple())
.init({
language: 'en',
// for development
apiUrl: import.meta.env.VITE_TOLGEE_API_URL,
apiKey: import.meta.env.VITE_TOLGEE_API_KEY,
// for production
staticData: {
...
}
});
</script>
<TolgeeProvider tolgee={tolgee}>
<div slot="fallback">Loading...</div>
<slot />
</TolgeeProvider>
The above code does the following:
- Imports the required methods and plugins from the integration library.
- Configures the Tolgee instance to use the DevTools and FormatSimple plugins, and initializes it using the credentials. It also sets the language to English.
- Wraps a component within the
TolgeeProvider
.
You can configure more options and plugins during initialization. Learn about other options and Tolgee plugins in their respective documentation.
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.