Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial example for i18n routing #18206

Merged
merged 18 commits into from Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/i18n-routing/.gitignore
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
23 changes: 23 additions & 0 deletions examples/i18n-routing/README.md
@@ -0,0 +1,23 @@
# Internationalized Routing

This example shows how to create internationalized pages using Next.js and the i18n routing feature. It shows a normal page, a non-dynamic `getStaticProps` page, a dynamic `getStaticProps` page, and a `getServerSideProps` page.

For further documentation on this feature see the documentation [here](https://nextjs.org/docs/advanced-features/i18n-routing)

## Deploy your own

Deploy the example using [Vercel](https://vercel.com):

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/amp)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npx create-next-app --example i18n-routing i18n-app
# or
yarn create next-app --example i18n-routing i18n-app
```

Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
8 changes: 8 additions & 0 deletions examples/i18n-routing/next.config.js
@@ -0,0 +1,8 @@
module.exports = {
experimental: {
ijjk marked this conversation as resolved.
Show resolved Hide resolved
i18n: {
locales: ['en', 'fr', 'nl'],
defaultLocale: 'en',
},
},
}
15 changes: 15 additions & 0 deletions examples/i18n-routing/package.json
@@ -0,0 +1,15 @@
{
"name": "amp",
ijjk marked this conversation as resolved.
Show resolved Hide resolved
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^16.7.0",
"react-dom": "^16.7.0"
},
"license": "MIT"
}
59 changes: 59 additions & 0 deletions examples/i18n-routing/pages/gsp/[slug].js
@@ -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
@@ -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
@@ -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
@@ -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
Expand Up @@ -15826,9 +15826,10 @@ styled-jsx-plugin-postcss@2.0.1:
postcss "^7.0.2"
postcss-load-plugins "^2.3.0"

styled-jsx@3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-3.3.0.tgz#32335c1a3ecfc923ba4f9c056eeb3d4699006b09"
styled-jsx@3.3.1:
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