Unverified Commit 9cbc4aac authored by Mohamed ElSaadany's avatar Mohamed ElSaadany Committed by GitHub

Add locale switcher to i18n-routing example (#27444)

* add locale switcher to dynamic GSP page in i18n-routing example

* Apply suggestions from code review
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>

* Add switcher to other pages as well
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 046f7164
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function LocaleSwitcher() {
const router = useRouter()
const { locales, locale: activeLocale } = router
const otherLocales = locales.filter((locale) => locale !== activeLocale)
return (
<div>
<p>Locale switcher:</p>
<ul>
{otherLocales.map((locale) => {
const { pathname, query, asPath } = router
return (
<li key={locale}>
<Link href={{ pathname, query }} as={asPath} locale={locale}>
<a>{locale}</a>
</Link>
</li>
)
})}
</ul>
</div>
)
}
import Link from 'next/link' import Link from 'next/link'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import LocaleSwitcher from '../../components/locale-switcher'
export default function GspPage(props) { export default function GspPage(props) {
const router = useRouter() const router = useRouter()
...@@ -17,6 +18,8 @@ export default function GspPage(props) { ...@@ -17,6 +18,8 @@ export default function GspPage(props) {
<p>Default locale: {defaultLocale}</p> <p>Default locale: {defaultLocale}</p>
<p>Configured locales: {JSON.stringify(props.locales)}</p> <p>Configured locales: {JSON.stringify(props.locales)}</p>
<LocaleSwitcher />
<Link href="/gsp"> <Link href="/gsp">
<a>To getStaticProps page</a> <a>To getStaticProps page</a>
</Link> </Link>
......
import Link from 'next/link' import Link from 'next/link'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import LocaleSwitcher from '../../components/locale-switcher'
export default function GspPage(props) { export default function GspPage(props) {
const router = useRouter() const router = useRouter()
...@@ -12,6 +13,8 @@ export default function GspPage(props) { ...@@ -12,6 +13,8 @@ export default function GspPage(props) {
<p>Default locale: {defaultLocale}</p> <p>Default locale: {defaultLocale}</p>
<p>Configured locales: {JSON.stringify(props.locales)}</p> <p>Configured locales: {JSON.stringify(props.locales)}</p>
<LocaleSwitcher />
<Link href="/gsp/first"> <Link href="/gsp/first">
<a>To dynamic getStaticProps page</a> <a>To dynamic getStaticProps page</a>
</Link> </Link>
......
import Link from 'next/link' import Link from 'next/link'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import LocaleSwitcher from '../components/locale-switcher'
export default function GsspPage(props) { export default function GsspPage(props) {
const router = useRouter() const router = useRouter()
...@@ -12,6 +13,8 @@ export default function GsspPage(props) { ...@@ -12,6 +13,8 @@ export default function GsspPage(props) {
<p>Default locale: {defaultLocale}</p> <p>Default locale: {defaultLocale}</p>
<p>Configured locales: {JSON.stringify(props.locales)}</p> <p>Configured locales: {JSON.stringify(props.locales)}</p>
<LocaleSwitcher />
<Link href="/gsp"> <Link href="/gsp">
<a>To getStaticProps page</a> <a>To getStaticProps page</a>
</Link> </Link>
......
import Link from 'next/link' import Link from 'next/link'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import LocaleSwitcher from '../components/locale-switcher'
export default function IndexPage(props) { export default function IndexPage(props) {
const router = useRouter() const router = useRouter()
...@@ -12,6 +13,8 @@ export default function IndexPage(props) { ...@@ -12,6 +13,8 @@ export default function IndexPage(props) {
<p>Default locale: {defaultLocale}</p> <p>Default locale: {defaultLocale}</p>
<p>Configured locales: {JSON.stringify(locales)}</p> <p>Configured locales: {JSON.stringify(locales)}</p>
<LocaleSwitcher />
<Link href="/gsp"> <Link href="/gsp">
<a>To getStaticProps page</a> <a>To getStaticProps page</a>
</Link> </Link>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment