Unverified Commit 8d97353d authored by Rubén Sospedra's avatar Rubén Sospedra Committed by GitHub

Add example with next-translate (#13511)

Motivated by the need to deploy static sites while having a good i18n support¹ ² @vinissimus developed [next-translate](https://github.com/vinissimus/next-translate).

Here's the example ;)

References:
1. https://github.com/vercel/next.js/discussions/10651
2. https://github.com/vercel/next.js/pull/12535#issuecomment-624317275
parent 16d22358
pages/
\ No newline at end of file
# next-translate examplle
This is an example of using [next-translate](https://github.com/vinissimus/next-translate) in a Next.js project.
## 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/with-next-translate)
## How to use
### Using `create-next-app`
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
npm init next-app --example with-next-translate with-next-translate-app
# or
yarn create next-app --example with-next-translate with-next-translate-app
```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-next-translate
cd with-next-translate
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
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)).
## Notes
This example is a basic starting point for using [next-translate](https://github.com/vinissimus/next-translate) with Next.js.
It's a tool to translate Next.js applications respecting the [Automatic Static Optimization](https://nextjs.org/docs/advanced-features/automatic-static-optimization). Deploy static sites with full i18n support!
import Link from 'next-translate/Link'
import Trans from 'next-translate/Trans'
import useTranslation from 'next-translate/useTranslation'
import Layout from '../components/Layout'
export default function Home() {
const { t } = useTranslation()
return (
<Layout>
<main>
<Trans
i18nKey="home:title"
components={[
<h1 className="title" />,
<a href="https://nextjs.org">Next.js!</a>,
]}
/>
<p className="description">
{t('home:description')} <code>_pages/index.js</code>
</p>
<div className="grid">
<Link href="/" lang="en">
<div className="card">
<h3>{t('home:english')}</h3>
<p>{t('home:change-english')}</p>
</div>
</Link>
<Link href="/" lang="ca">
<div className="card">
<h3>{t('home:catalan')}</h3>
<p>{t('home:change-catalan')}</p>
</div>
</Link>
<a href="https://nextjs.org/docs" className="card">
<h3>Next.js &rarr;</h3>
<p>{t('home:next-docs')}</p>
</a>
<a
href="https://github.com/vinissimus/next-translate"
className="card"
>
<h3>Learn &rarr;</h3>
<p>{t('home:plugin-docs')}</p>
</a>
</div>
</main>
<style jsx>{`
.title a {
color: #0070f3;
text-decoration: none;
}
.title a:hover,
.title a:focus,
.title a:active {
text-decoration: underline;
}
.title {
margin: 0;
line-height: 1.15;
font-size: 4rem;
}
.title,
.description {
text-align: center;
}
.description {
line-height: 1.5;
font-size: 1.5rem;
}
code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono,
DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
margin-top: 3rem;
}
.card {
margin: 1rem;
flex-basis: 45%;
padding: 1.5rem;
text-align: left;
color: inherit;
text-decoration: none;
border: 1px solid #eaeaea;
border-radius: 10px;
transition: color 0.15s ease, border-color 0.15s ease;
cursor: pointer;
}
.card:hover,
.card:focus,
.card:active {
color: #0070f3;
border-color: #0070f3;
}
.card h3 {
margin: 0 0 1rem 0;
font-size: 1.5rem;
}
.card p {
margin: 0;
font-size: 1.25rem;
line-height: 1.5;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
`}</style>
</Layout>
)
}
import React from 'react'
import Head from 'next/head'
import useTranslation from 'next-translate/useTranslation'
export default function Layout(props) {
const { t } = useTranslation()
return (
<div className="container">
<Head>
<title>{t('common:title')}</title>
<link rel="icon" href="/favicon.ico" />
</Head>
{props.children}
<footer>
<span>{t('common:powered')} </span>
<a href="https://vercel.com" target="_blank" rel="noopener noreferrer">
▲ vercel
</a>
<span>&amp;</span>
<a
href="https://github.com/vinissimus/next-translate"
target="_blank"
rel="noopener noreferrer"
>
next-translate
</a>
</footer>
<style jsx global>{`
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue,
sans-serif;
}
* {
box-sizing: border-box;
}
.container {
min-height: 100vh;
padding: 0 0.5rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
footer {
width: 100%;
height: 100px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
}
footer * {
margin-right: 0.5rem;
}
footer a:hover {
text-decoration: underline dashed #0070f3;
text-underline-position: under;
}
footer a {
color: inherit;
text-decoration: none;
}
.logo {
height: 1em;
}
`}</style>
</div>
)
}
{
"allLanguages": ["en", "ca"],
"defaultLanguage": "en",
"currentPagesDir": "_pages",
"finalPagesDir": "pages",
"localesPath": "locales",
"pages": {
"*": ["common"],
"/": ["home"]
}
}
{
"title": "Demostració with-next-translate",
"powered": "Powered by",
"alt": "Vercel logo"
}
{
"catalan": "Català",
"change-catalan": "Canvia a la versió en català",
"change-english": "Canvia a la versió en anglès",
"description": "Comença editant",
"english": "Anglès",
"next-docs": "Troba informació detallada sobre l'API de Next.js.",
"plugin-docs": "Llegeix més sobre com fer servir next-translate.",
"title": "<0>Benvingut a <1></1></0>"
}
{
"title": "Example with-next-translate",
"powered": "Powered by",
"alt": "Vercel logo"
}
{
"catalan": "Catalan",
"change-catalan": "Change language to Catalan",
"change-english": "Change language to English",
"description": "Get started by editing",
"english": "English",
"next-docs": "Find in-depth information about Next.js features and API.",
"plugin-docs": "Read more about next-translation usage.",
"title": "<0>Welcome to <1></1></0>"
}
{
"name": "with-next-translate",
"scripts": {
"dev": "next-translate && next dev",
"build": "next-translate && next build",
"start": "next start"
},
"dependencies": {
"next": "9.4.4",
"next-translate": "0.16.1",
"react": "16.13.1",
"react-dom": "16.13.1"
}
}
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