-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into feat/next-13-rsc
# Conflicts: # examples/example-next-13/src/components/LocaleSwitcher.tsx # packages/next-intl/package.json
- Loading branch information
Showing
24 changed files
with
382 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import VersionTabs from 'components/VersionTabs'; | ||
import Callout from 'components/Callout'; | ||
|
||
# Internationalization in Next.js error files | ||
|
||
The Next.js App Router's file convention provides two files that can be used for error handling: | ||
|
||
1. [`not-found.js`](https://nextjs.org/docs/app/api-reference/file-conventions/not-found) | ||
2. [`error.js`](https://nextjs.org/docs/app/api-reference/file-conventions/error) | ||
|
||
This page provides practical guides for these cases. | ||
|
||
<Callout> | ||
Have a look at [the App Router example](/examples/app-router) to explore a | ||
working app with error handling. | ||
</Callout> | ||
|
||
## `not-found.js` | ||
|
||
Next.js renders the closest `not-found` page when a route segment calls the [`notFound` function](https://nextjs.org/docs/app/api-reference/functions/not-found). Since the `[locale]` segment is used to localize your app, the `not-found` page needs to be located within this route segment too. | ||
|
||
```tsx filename="app/[locale]/not-found.tsx" | ||
import {useTranslations} from 'next-intl'; | ||
|
||
export default function NotFoundPage() { | ||
const t = useTranslations('NotFoundPage'); | ||
return <h1>{t('title')}</h1>; | ||
} | ||
``` | ||
|
||
Note however, that Next.js will only render this page when the `notFound` function is called from within a route, not for all unknown routes in general. | ||
|
||
### Catching unknown routes | ||
|
||
To catch unknown routes too, you can define a catch-all route that explicitly calls the `notFound` function. | ||
|
||
```tsx filename="app/[locale]/[...rest]/page.tsx" | ||
import {notFound} from 'next/navigation'; | ||
|
||
export default function CatchAllPage() { | ||
notFound(); | ||
} | ||
``` | ||
|
||
After this change, all requests that are matched within the `[locale]` segment will render the `not-found` page when an unknown route is encountered. | ||
|
||
### Catching non-localized requests | ||
|
||
When the user requests a route that is not matched by [the `next-intl` middleware](/docs/routing/middleware) (depending on your `matcher` config), there's no locale associated with the request (e.g. `/unknown.txt`). You can add a root `not-found` page to handle these cases too. | ||
|
||
```tsx filename="app/not-found.tsx" | ||
'use client'; | ||
|
||
import Error from 'next/error'; | ||
|
||
// Render the default Next.js 404 page when a route | ||
// is requested that doesn't match the middleware and | ||
// therefore doesn't have a locale associated with it. | ||
|
||
export default function NotFound() { | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
<Error statusCode={404} /> | ||
</body> | ||
</html> | ||
); | ||
} | ||
``` | ||
|
||
Note that a root layout file is required in this case, even if [it's just passing `children` through](https://github.com/amannn/next-intl/blob/main/examples/example-next-13/src/app/layout.tsx). | ||
|
||
## `error.js` | ||
|
||
When an `error` file is defined, Next.js creates [an error boundary within your layout](https://nextjs.org/docs/app/building-your-application/routing/error-handling#how-errorjs-works) that wraps pages accordingly to catch runtime errors: | ||
|
||
<figure> | ||
<div className="w-full lg:w-[500px]"> | ||
```tsx | ||
<LocaleLayout> | ||
<ErrorBoundary fallback={<Error />}> | ||
<Page /> | ||
</ErrorBoundary> | ||
</LocaleLayout> | ||
``` | ||
</div> | ||
|
||
<figcaption> | ||
Schematic component hierarchy that Next.js creates internally. | ||
</figcaption> | ||
|
||
</figure> | ||
|
||
Since the `error` file must be defined as a Client Component, you have to use [`NextIntlClientProvider`](/docs/configuration#client-server-components) to provide messages in case the `error` file renders. | ||
|
||
If you've [set up `next-intl` to be used in Client Components](/docs/getting-started/app-router-client-components), this is already the case and there's no additional setup needed. If you're using [the Server Components beta](/docs/getting-started/app-router-server-components) though, you have to provide the relevant messages in the wrapping layout. | ||
|
||
<figure> | ||
|
||
```tsx filename="app/[locale]/layout.tsx" | ||
import pick from 'lodash/pick'; | ||
|
||
export default async function LocaleLayout({children}) { | ||
// ... | ||
const messages = useMessages(); | ||
|
||
return ( | ||
<html lang={locale}> | ||
<body> | ||
<NextIntlClientProvider | ||
locale={locale} | ||
messages={pick(messages, 'Error')} | ||
> | ||
{children} | ||
</NextIntlClientProvider> | ||
</body> | ||
</html> | ||
); | ||
} | ||
``` | ||
|
||
<figcaption> | ||
Providing messages for the `error` file is only necessary when using [the | ||
Server Components beta](/docs/getting-started/app-router-server-components). | ||
</figcaption> | ||
|
||
</figure> | ||
|
||
Once `NextIntlClientProvider` is in place, you can use functionality from `next-intl` in the `error` file: | ||
|
||
```tsx filename="app/[locale]/error.tsx" | ||
'use client'; | ||
|
||
import {useTranslations} from 'next-intl'; | ||
|
||
export default function Error({error, reset}) { | ||
const t = useTranslations('Error'); | ||
|
||
return ( | ||
<div> | ||
<h1>{t('title')}</h1> | ||
<button onClick={reset}>{t('retry')}</button> | ||
</div> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ Next.js 13 introduces support for [React Server Components](https://nextjs.org/d | |
## Current beta version | ||
|
||
``` | ||
npm install [email protected].7 | ||
npm install [email protected].8 | ||
``` | ||
|
||
This beta version was tested with `[email protected]`. | ||
|
@@ -208,7 +208,7 @@ export default function Index() { | |
} | ||
``` | ||
|
||
2. The [APIs for using internationalization outside of components](#using-internationalization-outside-of-components) are integrated with static rendering. As a temporary solution, you can use these APIs in components too, but note that you have to "drill-down" the `locale` that is received via `params` to all components. | ||
2. The [APIs for using internationalization outside of components](/docs/environments/metadata-route-handlers) are integrated with static rendering. As a temporary solution, you can use these APIs in components too, but note that you have to "drill-down" the `locale` that is received via `params` to all components. Furthermore, the returned `t` function is based on [the `createTranslator` API](/docs/environments/core-library), meaning that `t.rich` uses functions that accept and return strings. | ||
|
||
```tsx filename="app/[locale]/page.tsx" | ||
import {getTranslator} from 'next-intl/server'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.