# gt-next: General Translation Next.js SDK: Tx URL: https://generaltranslation.com/en-GB/docs/next/api/components/tx.mdx --- title: Tx description: API reference for the Tx component --- ## Overview The `` component translates JSX content at runtime. ```jsx {/* [!code highlight] */} Today, I went to {" the store"}

to buy some groceries.

// [!code highlight] ``` The `` component supports translating plain text as well as complex JSX structures. Additionally, it provides features for handling variables, plurals, and context-specific translations. `` is server-side only. **Runtime Translation:** `` translations occur at runtime. This means translation will be performed live. *** ## Reference ### Props ### Descriptions | Prop | Description | | ---------- | ----------------------------------------------------------------------------------------------------------------------------- | | `children` | The content to be translated. This can include plain text or JSX structures. | | `context` | Additional context to refine the translation. Useful for resolving ambiguous phrases. | | `locale` | An optional locale to use for the translation. Will default to the browser's most preferred locale supported by your app. | | `maxChars` | An optional maximum character count for the translation. The library strictly enforces this limit. | *** ## Behaviour `` translates JSX at runtime. This means translations are performed live, so you can translate content that is only known at runtime. The trade-off is that there can be a noticeable delay while an on-demand translation loads. While loading, `` will return undefined unless the languages are similar (en-US vs en-GB), though this behaviour can be customised with render settings. If an error occurs, `` will return the original content. Our advice is to translate everything you can at build time using [``](/docs/next/api/components/t), [`getGT`](/docs/next/api/strings/use-gt), or [`useGT`](/docs/next/api/strings/use-gt), and only use on-demand translations, such as `` and [`tx`](/docs/next/api/strings/tx), when necessary. Make sure to follow the [deployment guide here](/docs/next/tutorials/quickdeploy). *** ## Examples ### Basic usage The `` component translates its children at runtime. ```jsx title="SimpleTranslation.jsx" copy import { Tx } from 'gt-next/server'; export default function Greeting() { return ( {/* [!code highlight] */} Hello, world! // [!code highlight] ); } ``` ### With variables You can use the `` component to mark children as variables. This allows you to mark content that should not be translated. ```jsx title="DynamicGreeting.jsx" copy import { Tx, Var } from 'gt-next/server'; export default function DynamicGreeting(user) { return ( {/* [!code highlight] */} Hello, {user.name} ); } ``` ### With plurals The `` component also supports pluralisation using the `` component. ```jsx title="ItemCount.jsx" copy import { Tx, Plural } from 'gt-next/server'; export default function ItemCount({ count }) { return ( You have an item.} // [!code highlight] other={<>You have items.} // [!code highlight] // [!code highlight] /> ); } ``` ### Limitations The `` function only translates its descendants. ```jsx title="Example.jsx" copy import { Tx } from 'gt-next/server'; const ValidTranslation = ({ children }) => (
{children}
); const InvalidTranslation = ({ children }) => (
No translation
); export default function Example() { return ( {/* [!code highlight] */}
This is valid!
// will be translated {/* [!code highlight] */} // will be translated {/* [!code highlight] */} Hello, world! {/* [!code highlight] */} // will not be translated
); } ``` **Note:** A good rule of thumb is that any content that is *literally* between the two `` tags in the file will be translated. You can always add another `` to translate content that is not being translated, though nesting `` components is not recommended. *** ## Notes * The `` component is designed for translating content in your application at runtime. * Use the `` component to translate plain text or JSX structures, including variables and pluralisation. ## Next steps * For build-time translations, look into the [``](/docs/next/api/components/t) component. * For more advanced features, see the [`` reference](/docs/next/guides/t). * For translating strings, look into [`tx`](/docs/next/api/strings/tx), [`getGT`](/docs/next/api/strings/get-gt), and [`useGT`](/docs/next/api/strings/use-gt).