1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Update with-react-i18next example (#3932)

- simplify pages by introducing withI18next hoc
- add Trans component example
- replace <div> with <p> in demo components to make text on pages easier to read

German translation by @marinaroot
This commit is contained in:
Alexander Kachkaev 2018-03-05 11:46:13 +00:00 committed by Tim Neutkens
parent 39be7183a5
commit dbb1b732a0
8 changed files with 73 additions and 64 deletions

View file

@ -0,0 +1,12 @@
import React from 'react'
import { Trans } from 'react-i18next'
export default function ComponentWithTrans () {
return (
<p>
<Trans i18nKey='common:transComponent'>
Alternatively, you can use <code>Trans</code> component.
</Trans>
</p>
)
}

View file

@ -1,14 +1,10 @@
import React from 'react' import React from 'react'
import { translate } from 'react-i18next' import { translate } from 'react-i18next'
function MyComponennt ({ t }) { function MyComponent ({ t }) {
return ( return <p>{t('extendedComponent')}</p>
<div>
{t('extendedComponent')}
</div>
)
} }
const Extended = translate('common')(MyComponennt) const Extended = translate('common')(MyComponent)
export default Extended export default Extended

View file

@ -1,10 +1,5 @@
import React from 'react' import React from 'react'
// pure component just getting t function by props
export default function PureComponent ({ t }) { export default function PureComponent ({ t }) {
return ( return <p>{t('common:pureComponent')}</p>
<div>
{t('common:pureComponent')}
</div>
)
} }

View file

@ -0,0 +1,24 @@
import { translate } from 'react-i18next'
import i18n from '../i18n'
export const withI18next = (namespaces = ['common']) => ComposedComponent => {
const Extended = translate(namespaces, { i18n, wait: process.browser })(
ComposedComponent
)
Extended.getInitialProps = async (ctx) => {
const composedInitialProps = ComposedComponent.getInitialProps
? await ComposedComponent.getInitialProps(ctx)
: {}
const i18nInitialProps =
ctx.req && !process.browser ? i18n.getInitialProps(ctx.req, namespaces) : {}
return {
...composedInitialProps,
...i18nInitialProps
}
}
return Extended
}

View file

@ -1,5 +1,6 @@
{ {
"integrates_react-i18next": "Dieses Beispiel integriert react-i18next für einfache Übersetzung.", "integrates_react-i18next": "Dieses Beispiel integriert react-i18next für einfache Übersetzung.",
"pureComponent": "Entweder t Funktion an Komponente via props weiterreichen.", "pureComponent": "Entweder t Funktion an Komponente via props weiterreichen.",
"extendedComponent": "Oder die Komponente erneut mit dem translate hoc erweiteren." "extendedComponent": "Oder die Komponente erneut mit dem translate hoc erweiteren.",
"transComponent": "Sonst können Sie auch die Komponente <1>Trans</1> verwenden."
} }

View file

@ -1,5 +1,6 @@
{ {
"integrates_react-i18next": "this sample integrates react-i18next for simple internationalization.", "integrates_react-i18next": "This example integrates react-i18next for simple internationalization.",
"pureComponent": "You can either pass t function to child components.", "pureComponent": "You can either pass t function to child components.",
"extendedComponent": "Or wrap your component using the translate hoc provided by react-i18next." "extendedComponent": "Or wrap your component using the translate hoc provided by react-i18next.",
"transComponent": "Alternatively, you can use <1>Trans</1> component."
} }

View file

@ -1,30 +1,20 @@
import React from 'react' import React from 'react'
import Link from 'next/link' import Link from 'next/link'
import { translate } from 'react-i18next'
import i18n from '../i18n'
import PureComponent from '../components/PureComponent' import PureComponent from '../components/PureComponent'
import ExtendedComponent from '../components/ExtendedComponent' import ExtendedComponent from '../components/ExtendedComponent'
import ComponentWithTrans from '../components/ComponentWithTrans'
import { withI18next } from '../lib/withI18next'
function Home ({ t, initialI18nStore }) { export default withI18next(['home', 'common'])(({ t, initialI18nStore }) => (
return (
<div> <div>
{t('welcome')} <h1>{t('welcome')}</h1>
<p>{t('common:integrates_react-i18next')}</p> <p>{t('common:integrates_react-i18next')}</p>
<PureComponent t={t} /> <PureComponent t={t} />
<ExtendedComponent /> <ExtendedComponent />
<Link href='/page2'><a>{t('link.gotoPage2')}</a></Link> <ComponentWithTrans />
<Link href='/page2'>
<a>{t('link.gotoPage2')}</a>
</Link>
</div> </div>
) ))
}
const Extended = translate(['home', 'common'], { i18n, wait: process.browser })(Home)
// Passing down initial translations
// use req.i18n instance on serverside to avoid overlapping requests set the language wrong
Extended.getInitialProps = async ({ req }) => {
if (req && !process.browser) return i18n.getInitialProps(req, ['home', 'common'])
return {}
}
export default Extended

View file

@ -1,30 +1,20 @@
import React from 'react' import React from 'react'
import Link from 'next/link' import Link from 'next/link'
import { translate } from 'react-i18next'
import i18n from '../i18n'
import PureComponent from '../components/PureComponent' import PureComponent from '../components/PureComponent'
import ExtendedComponent from '../components/ExtendedComponent' import ExtendedComponent from '../components/ExtendedComponent'
import ComponentWithTrans from '../components/ComponentWithTrans'
import { withI18next } from '../lib/withI18next'
function Page2 ({ t, initialI18nStore }) { export default withI18next(['page2', 'common'])(({ t, initialI18nStore }) => (
return (
<div> <div>
{t('welcomePage2')} <h1>{t('welcomePage2')}</h1>
<p>{t('common:integrates_react-i18next')}</p> <p>{t('common:integrates_react-i18next')}</p>
<PureComponent t={t} /> <PureComponent t={t} />
<ExtendedComponent /> <ExtendedComponent />
<Link href='/'><a>{t('link.gotoPage1')}</a></Link> <ComponentWithTrans />
<Link href='/'>
<a>{t('link.gotoPage1')}</a>
</Link>
</div> </div>
) ))
}
const Extended = translate(['page2', 'common'], { i18n, wait: process.browser })(Page2)
// Passing down initial translations
// use req.i18n instance on serverside to avoid overlapping requests set the language wrong
Extended.getInitialProps = async ({ req }) => {
if (req && !process.browser) return i18n.getInitialProps(req, ['page2', 'common'])
return {}
}
export default Extended