# gt-next: General Translation Next.js SDK: tx URL: https://generaltranslation.com/en-GB/docs/next/api/strings/tx.mdx --- title: tx description: API reference for the tx string translation function --- ## Overview The `tx` function is a server-side function for translating strings. ```jsx await tx('Hello, world!'); // returns 'Hola, mundo!' ``` **Runtime Translation:** `tx` translations occur at runtime. This means translation happens live, so you can translate content available at runtime. ## Reference ### Parameters | Name | Description | | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | | `content` | The string to be translated. | | `options` | Translation options for customising the behaviour of `tx`. See [`RuntimeTranslationOptions`](/docs/next/api/types/runtime-translation-options). | ### Returns A promise that resolves to a string containing the translated content, or the original content if no translation is required. *** ## Behaviour The `tx` function translates strings 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 is a delay while an on-demand translation loads, which is significantly slower. Our advice is to translate everything you can at build time using [`getGT`](/docs/next/api/strings/use-gt), [`useGT`](/docs/next/api/strings/use-gt), or [``](/docs/next/api/components/t), and only use on-demand translations, like `tx` and [``](/docs/next/api/components/tx), when necessary. Make sure to follow the [deployment guide here](/docs/next/tutorials/quickdeploy). *** ## Example ### Basic usage You can use `tx` to translate strings. ```javascript title="src/components/translateGreeting.jsx" copy import { tx } from 'gt-next/server'; export default async function translateGreeting() { return await tx("Hello, world!"); // [!code highlight] } ``` ### Adding context You can customise the translation by providing context to take into account during translation. ```javascript title="TranslateWithOptions.jsx" copy import { tx } from 'gt-next/server'; export default async function TranslateWithOptions() { return await tx("Hello, world!", { $context: 'Translate informally' // [!code highlight] }); } ``` ### Using variables To pass values to your string, you must (1) assign an identifier and (2) reference the identifier in the object you pass in. ```jsx title="translateWithVariables.js" copy // [!code word:price] import { tx } from 'gt-next/server'; export default async function translateWithVariables() { return await tx("The cost is {price, number, ::currency/USD}", { price: 29.99, }); } ``` ### Specifying a locale You can specify which locale to use for the translation. By default, the locale is set to the user's preferred language. ```jsx title="translateWithLocale.js" copy import { tx } from 'gt-next/server'; export default async function translateWithLocale() { return await tx("Hello, world!", { $locale: 'fr' }); // [!code highlight] } ``` *** ## Notes * `tx` is exclusively for server-side use and cannot be used in client-side components. * Translations with `tx` occur at runtime, meaning they are translated live. This is significantly slower than translations at build time. ## Next steps * See [`useGT`](/docs/next/api/strings/use-gt) and [`getGT`](/docs/next/api/strings/get-gt) for translating strings before deployment. * For translating JSX, see [``](/docs/next/api/components/t) and [``](/docs/next/api/components/tx). * See [`RuntimeTranslationOptions`](/docs/next/api/types/runtime-translation-options) for more information on customising translations.