diff --git a/examples/i18n-routing/.gitignore b/examples/i18n-routing/.gitignore new file mode 100644 index 000000000000000..1437c53f70bc211 --- /dev/null +++ b/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 diff --git a/examples/i18n-routing/README.md b/examples/i18n-routing/README.md new file mode 100644 index 000000000000000..fa6c4a9394a65b1 --- /dev/null +++ b/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)). diff --git a/examples/i18n-routing/next.config.js b/examples/i18n-routing/next.config.js new file mode 100644 index 000000000000000..f548199a3b10a8f --- /dev/null +++ b/examples/i18n-routing/next.config.js @@ -0,0 +1,6 @@ +module.exports = { + i18n: { + locales: ['en', 'fr', 'nl'], + defaultLocale: 'en', + }, +} diff --git a/examples/i18n-routing/package.json b/examples/i18n-routing/package.json new file mode 100644 index 000000000000000..656226b12945ae4 --- /dev/null +++ b/examples/i18n-routing/package.json @@ -0,0 +1,15 @@ +{ + "name": "i18n-routing", + "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" +} diff --git a/examples/i18n-routing/pages/gsp/[slug].js b/examples/i18n-routing/pages/gsp/[slug].js new file mode 100644 index 000000000000000..896537fe56cf4da --- /dev/null +++ b/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 ( +
+

getServerSideProps page

+

Current slug: {query.slug}

+

Current locale: {props.locale}

+

Default locale: {defaultLocale}

+

Configured locales: {JSON.stringify(props.locales)}

+ + + To getStaticProps page + +
+ + + To getServerSideProps page + +
+ + + To index page + +
+
+ ) +} + +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, + } +} diff --git a/examples/i18n-routing/pages/gsp/index.js b/examples/i18n-routing/pages/gsp/index.js new file mode 100644 index 000000000000000..4243a6034c467aa --- /dev/null +++ b/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 ( +
+

getServerSideProps page

+

Current locale: {props.locale}

+

Default locale: {defaultLocale}

+

Configured locales: {JSON.stringify(props.locales)}

+ + + To dynamic getStaticProps page + +
+ + + To getServerSideProps page + +
+ + + To index page + +
+
+ ) +} + +export const getStaticProps = ({ locale, locales }) => { + return { + props: { + locale, + locales, + }, + } +} diff --git a/examples/i18n-routing/pages/gssp.js b/examples/i18n-routing/pages/gssp.js new file mode 100644 index 000000000000000..94f7158b120209c --- /dev/null +++ b/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 ( +
+

getServerSideProps page

+

Current locale: {props.locale}

+

Default locale: {defaultLocale}

+

Configured locales: {JSON.stringify(props.locales)}

+ + + To getStaticProps page + +
+ + + To dynamic getStaticProps page + +
+ + + To index page + +
+
+ ) +} + +export const getServerSideProps = ({ locale, locales }) => { + return { + props: { + locale, + locales, + }, + } +} diff --git a/examples/i18n-routing/pages/index.js b/examples/i18n-routing/pages/index.js new file mode 100644 index 000000000000000..d3bd67204f267be --- /dev/null +++ b/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 ( +
+

Index page

+

Current locale: {locale}

+

Default locale: {defaultLocale}

+

Configured locales: {JSON.stringify(locales)}

+ + + To getStaticProps page + +
+ + + To dynamic getStaticProps page + +
+ + + To getServerSideProps page + +
+
+ ) +}