Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion js-sdk/integrations/react/next/app_router.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export default async function LocaleLayout({ children }: Props) {
The above code loads relevant locale available on the server and pass it to the client component through props.

:::info
Make sure you are not using `export const dynamic = 'force-static'` on your pages, as that makes it impossible to use cookies ([more info](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config)).
Make sure you are not using `export const dynamic = 'force-static'` on your pages, as that makes it impossible to use cookies ([more info](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config)). To render localized pages statically, keep the language in the URL instead: follow the [App router with next-intl](./app_router_next_intl.mdx) guide, which covers this in [Rendering Pages Statically](./app_router_next_intl.mdx#rendering-pages-statically).
:::

<AppRouterTranslating />
Expand Down
51 changes: 48 additions & 3 deletions js-sdk/integrations/react/next/app_router_next_intl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export function TolgeeBase() {
.updateDefaults({
apiKey,
apiUrl,
staticData: {
en: () => import('../../messages/en.json'),
cs: () => import('../../messages/cs.json'),
},
});
}

Expand Down Expand Up @@ -188,6 +192,7 @@ The next step is to wrap the children with the `TolgeeNextProvider`. Update the
```tsx title="src/app/[locale]/layout.tsx"
import React, { ReactNode } from 'react';
import { notFound } from 'next/navigation';
import { setRequestLocale } from 'next-intl/server';
import { TolgeeNextProvider } from '@/tolgee/client';
import { ALL_LANGUAGES } from '@/tolgee/shared';
import { getTolgee } from '@/tolgee/server';
Expand All @@ -197,11 +202,18 @@ type Props = {
params: Promise<{ locale: string }>;
};

export function generateStaticParams() {
return ALL_LANGUAGES.map((locale) => ({ locale }));
}

export default async function LocaleLayout({ children, params }: Props) {
const { locale } = await params;
if (!ALL_LANGUAGES.includes(locale)) {
notFound();
}
// required for static rendering
setRequestLocale(locale);

const tolgee = await getTolgee();
const records = await tolgee.loadRequired();

Expand Down Expand Up @@ -279,18 +291,51 @@ The `i18n/request.ts` is required by `next-intl` package, we don't actually need

```ts title="src/i18n/request.ts"
import { getRequestConfig } from 'next-intl/server';
import { DEFAULT_LANGUAGE } from '@/tolgee/shared';

export default getRequestConfig(async ({ requestLocale }) => {
const locale = await requestLocale;
const locale = (await requestLocale) ?? DEFAULT_LANGUAGE;
return {
// do this to make next-intl not emmit any warnings
messages: { locale: locale! },
// translations are handled by Tolgee, next-intl only needs the locale
locale,
messages: {},
};
});
```

<AppRouterTranslating />

## Rendering Pages Statically

To prerender your localized pages as static HTML at build time, export `generateStaticParams` from `src/app/[locale]/layout.tsx` and call [`setRequestLocale`](https://next-intl.dev/docs/routing/setup#static-rendering) in every layout and page under `[locale]`. The layout in [Use the `TolgeeNextProvider`](#use-the-tolgeenextprovider) already does both.

Without `setRequestLocale`, next-intl reads the locale from the request headers, and that makes the route dynamic. `getTranslate`, `getTolgee` and the server `T` component all read the locale this way, so call `setRequestLocale` before you use any of them. Layouts and pages render independently, so the call in the layout does not cover the pages.

Update `src/app/[locale]/page.tsx`, the page from [Localizing Server components](#localizing-server-components), as follows.

```tsx title="src/app/[locale]/page.tsx"
import { setRequestLocale } from 'next-intl/server';
import { getTranslate } from '@/tolgee/server';

type Props = {
params: Promise<{ locale: string }>;
};

export default async function IndexPage({ params }: Props) {
const { locale } = await params;
setRequestLocale(locale);

const t = await getTranslate();
return (
<main>
<h1>{t('hello_world')}</h1>
</main>
);
}
```

A statically rendered page contains the translations that were available when it was built. If you load translations from [Tolgee Content Delivery](#usage-of-tolgee-content-delivery), set `revalidate` as shown there, so that Next.js rebuilds the pages with new translations.

## Switching Languages

For switching languages you should use the `next-intl` router helpers.
Expand Down
2 changes: 1 addition & 1 deletion js-sdk/integrations/react/next/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ You can use the [Tolgee React SDK](../overview) to implement localization to you
- [Next.js with app router](./app-router)

Advanced
- [Next.js with next-intl and app router](./app-router-next-intl) - route based localization
- [Next.js with next-intl and app router](./app-router-next-intl) - route based localization, supports static rendering


> To learn to install the SDK follow the [React installation](../installation) documentation.
Loading