# General Translation React SDKs (gt-react, gt-next, gt-react-native): tx
URL: https://generaltranslation.com/zh/docs/react/nextjs/reference/functions/tx.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 使用 General Translation 在 Next.js App Router 的服务器端代码中翻译请求时字符串。tx 的 API 参考。

`tx` 函数通过 General Translation API 在请求时翻译字符串。它适用于在 build time 无法确定的 dynamic content；对于静态 source string，请使用 [`getGT`](/docs/react/nextjs/reference/functions/get-gt)。

`tx` 仅限服务器端使用，不支持 Pages Router。

## 概览 [#overview]

```tsx
import { tx } from 'gt-next/server';

const translated = await tx('A message loaded at request time');
```

## 参数 [#parameters]

| 参数        | 说明                        | 类型       | 可选 | 默认值  |
| --------- | ------------------------- | -------- | -- | ---- |
| `message` | 要翻译的字符串。                  | `string` | 否  | —    |
| `options` | 目标区域设置、上下文、格式、长度限制以及插值变量。 | `object` | 是  | `{}` |

`options` 接受 `$locale`、`$context`、`$maxChars` 和 `$format`。插值变量请作为顶层键传入。`$maxChars` 会向翻译服务请求正整数的最大长度，并在必要时将返回的字符串截断为该长度。

## 返回值 [#returns]

**类型** `Promise<string>`

一个 Promise，会解析为翻译后的字符串；如果翻译被禁用或无需翻译，则会解析为源字符串。

## 示例 [#example]

```tsx title="app/notes/[id]/page.tsx"
import { tx } from 'gt-next/server';

export default async function NotePage({ params }) {
  const note = await loadNote((await params).id);
  const translated = await tx(note, {
    $context: 'A user-created note',
  });
  return <p>{translated}</p>;
}
```

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
