-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add navigation APIs for App Router (
useRouter
, usePathname
…
…and `Link`) (#282) Changes: - Add navigation APIs for the App Router: `useRouter`, `usePathname` & `Link` (see [docs](https://next-intl-docs.vercel.app/docs/next-13)) - Revamp Next.js 13 example
- Loading branch information
Showing
51 changed files
with
1,076 additions
and
179 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
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,71 @@ | ||
import Callout from 'components/Callout'; | ||
|
||
# Next.js 13 internationalized navigation | ||
|
||
`next-intl` provides drop-in replacements for common Next.js navigation APIs that automatically handle the user locale behind the scenes. | ||
|
||
### `Link` | ||
|
||
This component wraps `next/link` and automatically prefixes the `href` with the current locale as necessary. If the default locale is matched, the `href` remains unchanged and no prefix is added. | ||
|
||
```tsx | ||
import {Link} from 'next-intl'; | ||
|
||
// When the user is on `/en`, the link will point to `/en/about` | ||
<Link href="/about">About</Link> | ||
|
||
// You can override the `locale` to switch to another language | ||
<Link href="/" locale="de">Switch to German</Link> | ||
``` | ||
|
||
### `useRouter` | ||
|
||
If you need to navigate programmatically, e.g. in response to a form submission, `next-intl` provides a convience API that wraps `useRouter` from Next.js and automatically applies the locale of the user. | ||
|
||
```tsx | ||
'use client'; | ||
|
||
import {useRouter} from 'next-intl/client'; | ||
|
||
const router = useRouter(); | ||
|
||
// When the user is on `/en`, the router will navigate to `/en/about` | ||
router.push('/about'); | ||
``` | ||
|
||
### `usePathname` | ||
|
||
To retrieve the pathname without a potential locale prefix, you can call `usePathname`. | ||
|
||
```tsx | ||
'use client'; | ||
|
||
import {usePathname} from 'next-intl/client'; | ||
|
||
// When the user is on `/en`, this will be `/` | ||
const pathname = usePathname(); | ||
``` | ||
|
||
### `redirect` | ||
|
||
<Callout type="warning"> | ||
This API is only available in [the Server Components | ||
beta](/docs/next-13/server-components). | ||
</Callout> | ||
|
||
If you want to interrupt the render of a Server Component and redirect to another page, you can invoke the `redirect` function from `next-intl/server`. This wraps [the `redirect` function from Next.js](https://beta.nextjs.org/docs/api-reference/redirect) and automatically applies the current locale. | ||
|
||
```tsx {1, 8} | ||
import {redirect} from 'next-intl/server'; | ||
|
||
export default async function Profile() { | ||
const user = await fetchUser(); | ||
|
||
if (!user) { | ||
// When the user is on `/en/profile`, this will be `/en/login` | ||
redirect('/login'); | ||
} | ||
|
||
// ... | ||
} | ||
``` |
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 |
---|---|---|
@@ -1,10 +1,14 @@ | ||
{ | ||
"next-13": { | ||
"title": "App Router", | ||
"theme": {"layout": "full"} | ||
}, | ||
"minimal": { | ||
"title": "Minimal setup", | ||
"title": "Pages Router", | ||
"theme": {"layout": "full"} | ||
}, | ||
"advanced": { | ||
"title": "Advanced usage", | ||
"title": "Pages Router (advanced)", | ||
"theme": {"layout": "full"} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
--- | ||
title: Advanced usage | ||
title: Pages Router (advanced) | ||
full: true | ||
--- | ||
|
||
import CodeSandbox from '../../components/CodeSandbox'; | ||
|
||
<CodeSandbox | ||
title="Advanced usage" | ||
title="Pages Router (advanced)" | ||
src="https://codesandbox.io/s/github/amannn/next-intl/tree/main/examples/example-advanced?file=/src/pages/index.tsx" | ||
/> |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
--- | ||
title: Minimal setup | ||
title: Pages Router | ||
full: true | ||
--- | ||
|
||
import CodeSandbox from '../../components/CodeSandbox'; | ||
|
||
<CodeSandbox | ||
title="Minimal setup" | ||
title="Pages Router" | ||
src="https://codesandbox.io/s/github/amannn/next-intl/tree/main/examples/example?file=/src/pages/index.tsx" | ||
/> |
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,11 @@ | ||
--- | ||
title: App Router | ||
full: true | ||
--- | ||
|
||
import CodeSandbox from '../../components/CodeSandbox'; | ||
|
||
<CodeSandbox | ||
title="App Router" | ||
src="https://codesandbox.io/s/github/amannn/next-intl/tree/main/examples/example-next-13?file=/src/app/[locale]/page.tsx" | ||
/> |
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 |
---|---|---|
@@ -1,12 +1,39 @@ | ||
{ | ||
"Index": { | ||
"title": "Start", | ||
"description": "Das ist die Startseite." | ||
"IndexPage": { | ||
"title": "next-intl Beispiel", | ||
"description": "Dies ist ein Beispiel, das die Verwendung von <code>next-intl</code> mit dem Next.js App Router demonstriert. Bei Ändern der Sprache rechts oben ändert sich der Inhalt dieser Seite." | ||
}, | ||
"AboutPage": { | ||
"title": "Über", | ||
"description": "<p>Auch das Routing ist internationalisiert.</p><p>Wenn du die Standardsprache Englisch verwendest, siehst du <code>/about</code> in der Adressleiste des Browsers auf dieser Seite.</p><p>Wenn du die Sprache auf Deutsch änderst, wird die URL mit der Locale ergänzt (<code>/de/about</code>).</p>" | ||
}, | ||
"NotFoundPage": { | ||
"title": "Seite nicht gefunden", | ||
"description": "Bitte überprüfe die URL oder verwende die Navigation um zu einer bekannten Seite zu wechseln." | ||
}, | ||
"LocaleLayout": { | ||
"title": "next-intl Beispiel" | ||
}, | ||
"LocaleSwitcher": { | ||
"switchLocale": "Zu {locale, select, de {Deutsch} en {Englisch} other {Unbekannt}} wechseln" | ||
"label": "Sprache ändern", | ||
"locale": "{locale, select, de {Deutsch} en {Englisch} other {Unbekannt}}" | ||
}, | ||
"Navigation": { | ||
"home": "Start", | ||
"about": "Über" | ||
}, | ||
"PageLayout": { | ||
"links": { | ||
"docs": { | ||
"title": "Dokumentation", | ||
"description": "Erfahre mehr über next-intl in der offiziellen Dokumentation.", | ||
"href": "https://next-intl-docs.vercel.app/" | ||
}, | ||
"source": { | ||
"title": "Quellcode", | ||
"description": "Sieh dir den Quellcode dieses Beispiels auf GitHub an.", | ||
"href": "https://github.com/amannn/next-intl/tree/main/examples/example-next-13" | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,39 @@ | ||
{ | ||
"Index": { | ||
"title": "Home", | ||
"description": "This is the home page." | ||
"IndexPage": { | ||
"title": "next-intl example", | ||
"description": "This is a basic example that demonstrates the usage of <code>next-intl</code> with the Next.js App Router. Try changing the locale in the top right corner and see how the content changes." | ||
}, | ||
"AboutPage": { | ||
"title": "About", | ||
"description": "<p>The routing is internationalized too.</p><p>If you're using the default language English, you'll see <code>/about</code> in the browser address bar on this page.</p><p>If you change the locale to German, the URL is prefixed with the locale (<code>/de/about</code>).</p>" | ||
}, | ||
"NotFoundPage": { | ||
"title": "Page not found", | ||
"description": "Please double-check the URL or use the navigation to go to a known page." | ||
}, | ||
"LocaleLayout": { | ||
"title": "next-intl example" | ||
}, | ||
"LocaleSwitcher": { | ||
"switchLocale": "Switch to {locale, select, de {German} en {English} other {Unknown}}" | ||
"label": "Change language", | ||
"locale": "{locale, select, de {German} en {English} other {Unknown}}" | ||
}, | ||
"Navigation": { | ||
"home": "Home", | ||
"about": "About" | ||
}, | ||
"PageLayout": { | ||
"links": { | ||
"docs": { | ||
"title": "Docs", | ||
"description": "Learn more about next-intl in the official docs.", | ||
"href": "https://next-intl-docs.vercel.app/" | ||
}, | ||
"source": { | ||
"title": "Source code", | ||
"description": "Browse the source code of this example on GitHub.", | ||
"href": "https://github.com/amannn/next-intl/tree/main/examples/example-next-13" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
e30a89b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-intl-example-next-13 – ./examples/example-next-13
next-intl-example-next-13.vercel.app
next-intl-example-next-13-amannn.vercel.app
next-intl-example-next-13-git-main-amannn.vercel.app
e30a89b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-intl-docs – ./docs
next-intl-docs-git-main-amannn.vercel.app
next-intl-docs.vercel.app
next-intl-docs-amannn.vercel.app
e30a89b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
example-next-13-next-auth – ./examples/example-next-13-next-auth
example-next-13-next-auth.vercel.app
example-next-13-next-auth-git-main-amannn.vercel.app
example-next-13-next-auth-amannn.vercel.app