General Translation  
Next.jsComponents

<T>

API Reference for the <T> component

Overview

The <T> component is the main translation method in gt-next. Its contents are translated into a target language. Every <T> component has a unique id which is used to cache translations.

<T id="example">
    Today, I went to
    {" the store"}
    <p>
        to <b>buy</b> some <i>groceries</i>.
    </p>
</T> 

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

PropTypeDefault
children
any
-
id
string
-
context?
string
undefined
variables?
Record<string, any>
undefined
renderSettings?
object
{}

Descriptions

PropDescription
childrenThe content to be translated. This can include plain text or JSX structures.
idA unique identifier for the translation string. This ensures consistent translation across your app.
contextAdditional context to refine the translation. Useful for resolving ambiguous phrases.
variablesA 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.
renderSettingsOptional 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:

  1. 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.
  2. 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.

SimpleTranslation.jsx
import { T } from 'gt-next';
 
export default function Greeting() {
    return (
        <T id="greeting">
            Hello, world!
        </T> 
    );
}

With variables

The <T> component can include variables for dynamic content within translations.

DynamicGreeting.jsx
import { T, Var } from 'gt-next';
 
export default function DynamicGreeting(user) {
    return (
        <T id="greeting">
            Hello, <Var>{user.name}</Var>!
        </T>
    );
}

With plurals

The <T> component also supports pluralization using the <Plural> component.

ItemCount.jsx
import { T, Plural } from 'gt-next';
 
export default function ItemCount({ count }) {
    return (
        <T id="item_count">
            <Plural n={count}  
                singular={<>You have an item.</>}  
                plural={<>You have items.</>}  
            />
        </T>
    );
}

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 in gt-next.
  • 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

On this page