Overview
Overview of General Translation's React SDK
Introduction
The General Translation React SDK is an open-source internationalization (i18n) library for React.
It offers a set of tools to help you internationalize your React application in a easy and maintainable way, with feature parity to other popular i18n libraries.
The React SDK can be used without the General Translation platform, and will act very similarly to other i18n libraries.
However, it also integrates with our platform, offering additional features such as:
- Translation Hot Reloading in Development
- Automatic AI translations
- Syncing translations with the General Translation platform
- Native integration with our translation CDN
Concepts
There are 4 main concepts to understand about the SDK.
The <GTProvider>
component
The <T>
component
The useGT
hook
(Optional) The useDict
hook
The <GTProvider>
component
import { GTProvider } from 'gt-react';
The <GTProvider>
component is the main component that you need to add to your application.
It is used to provide the rest of your application with context.
This context includes the current language and the relevant translations for that language.
Important Considerations
- The provider should wrap your entire application.
- Ideally, it should be placed as high in the tree as possible, such as in your root component.
See the GTProvider page for more information.
The <T>
component
The <T>
component is the recommended way to translate content in your application.
It is a React component that can be used to wrap any JSX element, and will automatically render the content of the element into a supported language.
We recommend using the <T>
component wherever possible, since it allows for the most flexibility in translations.
Unlike strings, the <T>
component can be used to translate HTML content, making it much more powerful than string translations.
Examples
<T>
<div>Hello, world!</div>
</T>
<T>
<div>
<h1> Here is an image </h1>
<img src="https://example.com/image.png" alt="Example" />
</div>
</T>
<T>
Formatting can be done easily with the `<T>` component.
<Num>{1000}</Num>
<DateTime>{new Date()}</DateTime>
<Currency>{1000}</Currency>
</T>
See the translating JSX guide to learn about the different ways to use the <T>
component.
See the T API Reference for the API of the <T>
component.
The useGT
hook
The useGT
hook is a React hook that can be used similarly to the <T>
component, with some trade-offs.
The hook returns a function that can be used to translate strings.
const translate = useGT();
translate('Hello, world!');
Compared to the <T>
component, the useGT
hook allows for more flexibility in your codebase.
For example, if you have a complex data structure with nested strings, a <T>
component would be more difficult to use.
const t = useGT();
const data = {
title: t('Hello, world!'),
description: t('This is a description'),
};
See the strings guide to learn more about the useGT
hook.
See the useGT API Reference for more information.
The useDict
hook
The useDict
hook is a React hook that returns a function that can be used to retrieve translations for a given key.
const dictionary = {
hello: {
world: 'Hello, world!',
},
};
const translate = useDict();
translate('hello.world');
This behavior is similar to other translation libraries, such as react-i18next
and next-intl
.
We do not recommend using the useDict
hook in your application. Frequent use of the useDict
hook will make your codebase more difficult to maintain,
and will lead to large tech debt.
Instead, we recommend using the <T>
component or the useGT
hook.
If you are migrating from another i18n library, the useDict
hook is a drop-in replacement and can useful for incrementally migrating your codebase.
See the dictionaries guide to learn more about the useDict
hook.
See the useDict API Reference for more information.
Next Steps
- Learn about how to setup your React project with the SDK: Project Quickstart
- Learn about how to translate strings with the
useGT
hook: Translating Strings - Learn about how to translate JSX content with the
<T>
component: Translating JSX
How is this guide?