Skip to content

Commit

Permalink
Add example pages for i18n routing
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Oct 24, 2020
1 parent 31cc439 commit 6f30287
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 3 deletions.
8 changes: 8 additions & 0 deletions examples/i18n-routing/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
experimental: {
i18n: {
locales: ['en', 'fr', 'nl'],
defaultLocale: 'en',
},
},
}
59 changes: 59 additions & 0 deletions examples/i18n-routing/pages/gsp/[slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Link from 'next/link'
import { useRouter } from 'next/router'

export default function GspPage(props) {
const router = useRouter()
const { defaultLocale, isFallback, query } = router

if (isFallback) {
return 'Loading...'
}

return (
<div>
<h1>getServerSideProps page</h1>
<p>Current slug: {query.slug}</p>
<p>Current locale: {props.locale}</p>
<p>Default locale: {defaultLocale}</p>
<p>Configured locales: {JSON.stringify(props.locales)}</p>

<Link href="/gsp">
<a>To getStaticProps page</a>
</Link>
<br />

<Link href="/gssp">
<a>To getServerSideProps page</a>
</Link>
<br />

<Link href="/">
<a>To index page</a>
</Link>
<br />
</div>
)
}

export const getStaticProps = ({ locale, locales }) => {
return {
props: {
locale,
locales,
},
}
}

export const getStaticPaths = ({ locales }) => {
const paths = []

for (const locale of locales) {
paths.push({ params: { slug: 'first' }, locale })
paths.push({ params: { slug: 'second' }, locale })
}

return {
paths,
fallback: true,
}
}
40 changes: 40 additions & 0 deletions examples/i18n-routing/pages/gsp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Link from 'next/link'
import { useRouter } from 'next/router'

export default function GspPage(props) {
const router = useRouter()
const { defaultLocale } = router

return (
<div>
<h1>getServerSideProps page</h1>
<p>Current locale: {props.locale}</p>
<p>Default locale: {defaultLocale}</p>
<p>Configured locales: {JSON.stringify(props.locales)}</p>

<Link href="/gsp/first">
<a>To dynamic getStaticProps page</a>
</Link>
<br />

<Link href="/gssp">
<a>To getServerSideProps page</a>
</Link>
<br />

<Link href="/">
<a>To index page</a>
</Link>
<br />
</div>
)
}

export const getStaticProps = ({ locale, locales }) => {
return {
props: {
locale,
locales,
},
}
}
40 changes: 40 additions & 0 deletions examples/i18n-routing/pages/gssp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Link from 'next/link'
import { useRouter } from 'next/router'

export default function GsspPage(props) {
const router = useRouter()
const { defaultLocale } = router

return (
<div>
<h1>getServerSideProps page</h1>
<p>Current locale: {props.locale}</p>
<p>Default locale: {defaultLocale}</p>
<p>Configured locales: {JSON.stringify(props.locales)}</p>

<Link href="/gsp">
<a>To getStaticProps page</a>
</Link>
<br />

<Link href="/gsp/first">
<a>To dynamic getStaticProps page</a>
</Link>
<br />

<Link href="/">
<a>To index page</a>
</Link>
<br />
</div>
)
}

export const getServerSideProps = ({ locale, locales }) => {
return {
props: {
locale,
locales,
},
}
}
31 changes: 31 additions & 0 deletions examples/i18n-routing/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Link from 'next/link'
import { useRouter } from 'next/router'

export default function IndexPage(props) {
const router = useRouter()
const { locale, locales, defaultLocale } = router

return (
<div>
<h1>Index page</h1>
<p>Current locale: {locale}</p>
<p>Default locale: {defaultLocale}</p>
<p>Configured locales: {JSON.stringify(locales)}</p>

<Link href="/gsp">
<a>To getStaticProps page</a>
</Link>
<br />

<Link href="/gsp/first">
<a>To dynamic getStaticProps page</a>
</Link>
<br />

<Link href="/gssp">
<a>To getServerSideProps page</a>
</Link>
<br />
</div>
)
}
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15826,9 +15826,10 @@ [email protected]:
postcss "^7.0.2"
postcss-load-plugins "^2.3.0"

[email protected]:
version "3.3.0"
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-3.3.0.tgz#32335c1a3ecfc923ba4f9c056eeb3d4699006b09"
[email protected]:
version "3.3.1"
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-3.3.1.tgz#d79f306c42c99cefbe8e76f35dad8100dc5c9ecc"
integrity sha512-RhW71t3k95E3g7Zq3lEBk+kmf+p4ZME7c5tfsYf9M5mq6CgIvFXkbvhawL2gWriXLRlMyKAYACE89Qa2JnTqUw==
dependencies:
"@babel/types" "7.8.3"
babel-plugin-syntax-jsx "6.18.0"
Expand Down

0 comments on commit 6f30287

Please sign in to comment.