# gt-next: General Translation Next.js SDK: tx URL: https://generaltranslation.com/en-US/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 will be performed live, so you can translate content known at runtime. ## Reference ### Parameters | Name | Description | |-----------------------|-----------------------------------------------------------------------------| | `content` | The string that needs to be translated. | | `options` | Translation options to customize the behavior 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 needed. --- ## Behavior The `tx` function translates strings at runtime. This means that 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 waiting for an on-demand translation to load 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 customize the translation by providing a context to be considered when translating. ```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 In order to pass values to your string, you must (1) assign an identifier and (2) reference the identifier in the passed object. ```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 a 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` exclusively for server-side usage 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 customizing translations.