> For the complete documentation index, see [llms.txt](https://developers.weglot.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.weglot.com/frameworks-and-ai/react/dynamic-content.md).

# Dynamic content

Translate content from a CMS, an API or your users, server-side.

The plugin only translates text written in your code. Content it never sees, such as a CMS body, a search result or a user's own words, is translated server-side during the render, with the functions of `@weglot/translate-react/dynamic`:

| Function             | Translates                                   |
| -------------------- | -------------------------------------------- |
| `tDynamic(text)`     | One text.                                    |
| `tDynamicAll(texts)` | Several texts, in one request.               |
| `tDynamicHtml(html)` | An HTML fragment, keeping its inline markup. |

These functions only run server-side, in Next.js server components: they use your API key to call the Weglot API and read the visitor's language from the request. They read the languages from the plugin, so the plugin must be installed as described in [Getting started](https://developers.weglot.com/frameworks-and-ai/react/getting-started.md).

Each function expects a string: pass `?? ""` for fields that can be empty. Start the calls of a page together, with `Promise.all`, so they share one request:

```tsx
// app/blog/[slug]/page.tsx
import { tDynamic, tDynamicHtml } from "@weglot/translate-react/dynamic";

export default async function PostPage({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const post = await getPost(slug);

  const [title, body] = await Promise.all([
    tDynamic(post.title ?? ""),
    tDynamicHtml(post.body ?? ""),
  ]);

  return (
    <article>
      <h1>{title}</h1>
      <div dangerouslySetInnerHTML={{ __html: body }} />
    </article>
  );
}
```

Translate SEO fields in `generateMetadata` the same way:

```tsx
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const post = await getPost(slug);
  const [title, description] = await tDynamicAll([post.seoTitle ?? "", post.seoDescription ?? ""]);
  return { title, description };
}
```

## When the source text is returned

A slow call never holds up your page. These functions return the source text:

* **When the API takes longer than 500 ms.** Calls started together are batched into one request, which shares this timeout. A call awaited after another one waits for its own request.
* **The first time a text is requested.** Weglot generates the translation, and later requests get it.

{% hint style="warning" %}
If you cache data yourself, for example with `unstable_cache`, the `fetch` cache or a CDN, make the cache depend on the visitor's language. Otherwise a text is cached in one language, possibly the source one, and served to every visitor.
{% endhint %}

## Options

Each function accepts options as its last argument:

* `timeoutMs`: how long to wait for the API, in milliseconds (default `500`).
* `locale`: translate to this language instead of the visitor's.

```ts
const title = await tDynamic(post.title, { timeoutMs: 2000 });
```

If your API key does not come from the `WEGLOT_API_KEY` environment variable, set it once with `configureDynamicTranslation({ apiKey })`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.weglot.com/frameworks-and-ai/react/dynamic-content.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
