Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Pages Router API routes #1161

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/pages/docs/environments/actions-metadata-route-handlers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,56 @@ export async function GET(request) {
return NextResponse.json({title: t('title')});
}
```

### Pages Router API Routes

You cannot use `next-intl` with API routes in the pages router as you would with route handlers. If you tried you may have run into the following error:

```
`getTranslations` is not supported in Client Components
```

Instead, you have to manually import the messages (as you would with a "page" in the pages router) and use the `createTranslator` function to create your `t` variable.

Below is an example implementation of the `getTranslations` function that would work with pages router:

```ts filename="/lib/intl-server.ts"
import { createTranslator } from "next-intl";

/**
* Workaround to use `getTranslations` in API routes in the pages router.
*
* @param opts options containing locale (required) and namespace.
* @returns translator
*/
export async function getTranslations(opts: {
locale: string;
namespace?: string;
}) {
const messages = (await import(`../messages/${opts.locale}.json`)).default;

return createTranslator({
locale: opts.locale,
messages,
namespace: opts.namespace,
});
}
```

Then use it as you would normally:

```ts filename="/pages/api/some-route.ts"
import { getTranslations } from "@/lib/intl-server";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
// Example: Receive the `locale` via the `accept-language` HTTP header, or
// fallback to English.
const locale = req.headers['accept-language'] ?? 'en';
const t = getTranslations({locale, namespace: 'Hello'});

return res.json({title: t('title')})
}
```
Loading