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

consistent I18nProvider and enable client side translation loading #1170

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export const getStaticProps = async ({ locale }) => ({
})
```

### 6. Loading translations - Client side

While loading your translations via `getServerSideProps` and `getStaticProps` is recommended if you need to load them client side then you can see the example [here](examples/withClientsideHttp)

#### Options

| Key | Default value |
Expand Down
5 changes: 5 additions & 0 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# A simple example of next-i18next

This example shows `next-i18next` being used simply for loading translations in a statically generated site usinge the `serverSideTranslations` method

Please see the [main Readme](https://github.com/isaachinman/next-i18next/blob/master/README.md#next-i18next) for more information
3 changes: 2 additions & 1 deletion examples/simple/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { appWithTranslation } from 'next-i18next'
import nextI18nConfig from '../next-i18next.config'

const MyApp = ({ Component, pageProps }) => <Component {...pageProps} />

export default appWithTranslation(MyApp)
export default appWithTranslation(MyApp, nextI18nConfig)
22 changes: 21 additions & 1 deletion examples/simple/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'

Expand All @@ -11,6 +12,17 @@ const Homepage = () => {

const router = useRouter()
const { t } = useTranslation('common')
const [counter, setCounter] = useState(1)

useEffect(() => {
if (router.query.counter) {
setCounter(parseInt(router.query.counter))
}
},[router.query.counter])

const updateShallowRoute = () => {
router.push(`/?counter=${counter + 1}`, undefined, {shallow: true})
}

return (
<>
Expand All @@ -32,14 +44,22 @@ const Homepage = () => {
{t('to-second-page')}
</button>
</Link>
<button
type='button'
onClick={updateShallowRoute}
>
{t('shallow-route', {counter: counter + 1})}
</button>
</div>
</main>
<Footer />
</>
)
}

export const getStaticProps = async ({ locale }) => ({
export const getStaticProps = async ({
locale,
}) => ({
props: {
...await serverSideTranslations(locale, ['common', 'footer']),
},
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/pages/second-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const SecondPage = () => {
)
}

export const getStaticProps = async ({ locale }) => ({
export const getStaticProps = async ({locale}) => ({
props: {
...await serverSideTranslations(locale, ['second-page', 'footer']),
},
Expand Down
3 changes: 2 additions & 1 deletion examples/simple/public/locales/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"to-second-page": "Zur zweiten Seite",
"error-with-status": "Auf dem Server ist ein Fehler ({{statusCode}}) aufgetreten",
"error-without-status": "Auf dem Server ist ein Fehler aufgetreten",
"title": "Hauptseite | next-i18next"
"title": "Hauptseite | next-i18next",
"shallow-route": "zur flache route {{counter}}"
}
3 changes: 2 additions & 1 deletion examples/simple/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"to-second-page": "To second page",
"error-with-status": "A {{statusCode}} error occurred on server",
"error-without-status": "An error occurred on the server",
"title": "Home | next-i18next"
"title": "Home | next-i18next",
"shallow-route": "To shallow route {{counter}}"
}
Loading