<T>
API Reference for the <T> component
Overview
The <T>
component is the main translation method in gt-react
.
Its contents are translated into a target language.
Every <T>
component has a unique id
which is used to cache translations.
The <T>
component supports translating plain text as well as complex JSX structures.
Additionally, it provides features for handling variables, plurals, and context-specific translations.
For production, to ensure translations are available, run the CLI tool before deploying your app.
Reference
Props
Prop | Type | Default |
---|---|---|
children | any | - |
id | string | - |
context? | string | undefined |
variables? | Record<string, any> | undefined |
renderSettings? | object | {} |
Descriptions
Prop | Description |
---|---|
children | The content to be translated. This can include plain text or JSX structures. |
id | A unique identifier for the translation string. This ensures consistent translation across your app. |
context | Additional context to refine the translation. Useful for resolving ambiguous phrases. |
variables | A mapping of variables to include in the translation. These are replaced dynamically in the output. It is recommended to use inline <Currency> , <DateTime> , <Num> , and <Var> instead. |
renderSettings | Optional settings to control how fallback content is rendered during translation loading. |
Returns
React.JSX.Element|undefined
which contains the rendered translation or fallback content based on the provided configuration.
Best Practices
Dynamic content
Wrap all dynamic content inside of <Currency>
, <DateTime>
, <Num>
, or <Var>
components.
The children of the <T>
component are translated via our API.
All content inside of <T>
should be static.
This has two benefits:
- Efficiency: Translations are generated when content changes. Instead of creating a new translation each time a value changes, only one translation is generated, and the dynamic content is inserted to the translation at render time.
- Privacy: The children of
<Currency>
,<DateTime>
,<Num>
, or<Var>
are NEVER sent to the translation API. Because the children of the<T>
component are sent to the translation API, if you have sensitive information within a<T>
tag, wrap it in one of the variable tags.
Do not pass sensitive or private information DIRECTLY to the <T>
component.
Wrap all sensitive information with a <Currency>
, <DateTime>
, <Num>
, or <Var>
.
Nested components
<T>
will only translate of its static children.
If you have a component that needs to return some JSX content, then that component will need its own <T>
wrapper.
You can read more about this topic here.
Examples
Basic usage
The <T>
component can translate simple strings using an id
and its children.
Remember, the <T>
component must be used inside a <GTProvider>
to access the translations.
With variables
The <T>
component can include variables for dynamic content within translations.
With plurals
The <T>
component also supports pluralization using the <Plural>
component.
Text must be passed directly as a child of <T>
to be translated.
Wrapping text in other components without providing context will result in untranslated output.
Notes
- The
<T>
component is designed for translating content in your application. It is the primary method for localization ingt-react
. - Use the
<T>
component to translate plain text or JSX structures, including variables and pluralization. - Ensure the
<T>
component is wrapped in a<GTProvider>
to access the translation context.
Next steps
- Look into more advanced features like on-demand translation, variables, context, and handling plurals, refer to the
<T>
Design Patterns documentation. - For translating strings, look into
tx()
orgetGT()
for server-side translations anduseGT()
for client-side translations. - Checkout using variable components and using branching components for more advanced translation patterns.