Skip to content

Commit

Permalink
extend next example
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jul 16, 2022
1 parent 2c66966 commit 1b50dbb
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 3 deletions.
12 changes: 9 additions & 3 deletions example/next/next-i18next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ const HttpBackend = require('i18next-http-backend/cjs')
const ChainedBackend= require('i18next-chained-backend').default
const LocalStorageBackend = require('i18next-localstorage-backend').default

const isBrowser = typeof window !== 'undefined'

module.exports = {
debug: process.env.NODE_ENV === 'development',
backend: {
backendOptions: [{ expirationTime: 60 * 60 * 1000 }, {}], // 1 hour
backends: typeof window !== 'undefined' ? [LocalStorageBackend, HttpBackend]: [],
backends: isBrowser ? [LocalStorageBackend, HttpBackend]: [],
},
debug: process.env.NODE_ENV === 'development',
// react: { // used only for the lazy reload
// bindI18n: 'languageChanged loaded',
// useSuspense: false
// },
i18n: {
defaultLocale: 'en',
locales: ['en', 'de'],
},
serializeConfig: false,
use: typeof window !== 'undefined' ? [ChainedBackend] : [],
use: isBrowser ? [ChainedBackend] : [],
}
1 change: 1 addition & 0 deletions example/next/pages/client-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Footer } from '../components/Footer'
const ClientPage = () => {
const { t, ready } = useTranslation(['client-page', 'footer'])
if (!ready) return 'loading translations...'
// but because of this ready return, you may see a warning like this: "Expected server HTML to contain a matching text node for..."

return (
<>
Expand Down
7 changes: 7 additions & 0 deletions example/next/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ const Homepage = () => {
{t('to-client-page')}
</button>
</Link>
<Link href='/lazy-reload-page'>
<button
type='button'
>
{t('to-lazy-reload-page')}
</button>
</Link>
<button
type='button'
onClick={updateShallowRoute}
Expand Down
42 changes: 42 additions & 0 deletions example/next/pages/lazy-reload-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Link from 'next/link'

import { useTranslation } from 'next-i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'

import { Header } from '../components/Header'
import { Footer } from '../components/Footer'
import { useEffect } from 'react'

const LazyReloadPage = () => {

const { t, i18n } = useTranslation(['lazy-reload-page', 'footer'], { bindI18n: 'languageChanged loaded' })
// bindI18n: loaded is needed because of the reloadResources call
// if all pages use the reloadResources mechanism, the bindI18n option can also be defined in next-i18next.config.js
useEffect(() => {
i18n.reloadResources(i18n.resolvedLanguage, ['lazy-reload-page', 'footer'])
}, [])

return (
<>
<main>
<Header heading={t('h1')} title={t('title')} />
<Link href='/'>
<button
type='button'
>
{t('back-to-home')}
</button>
</Link>
</main>
<Footer />
</>
)
}

export const getStaticProps = async ({ locale }) => ({
props: {
...await serverSideTranslations(locale, ['lazy-reload-page', 'footer']),
},
})

export default LazyReloadPage
1 change: 1 addition & 0 deletions example/next/public/locales/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"change-locale": "Wechseln Locale",
"to-second-page": "Zur zweiten Seite",
"to-client-page": "Zur Client Seite",
"to-lazy-reload-page": "Zur Lazy-Reload 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",
Expand Down
5 changes: 5 additions & 0 deletions example/next/public/locales/de/lazy-reload-page.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"h1": "Eine Seite, die Übersetzungsressourcen über http aktualisiert nachlädt",
"back-to-home": "Zurück zur Hauptseite",
"title": "Lazy Reload-Seite | next-i18next"
}
1 change: 1 addition & 0 deletions example/next/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"change-locale": "Change locale",
"to-second-page": "To second page",
"to-client-page": "To client page",
"to-lazy-reload-page": "To lazy reload page",
"error-with-status": "A {{statusCode}} error occurred on server",
"error-without-status": "An error occurred on the server",
"title": "Home | next-i18next",
Expand Down
5 changes: 5 additions & 0 deletions example/next/public/locales/en/lazy-reload-page.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"h1": "A page that lazy realoads translation resources via http",
"back-to-home": "Back to home",
"title": "Lazy reload page | next-i18next"
}

0 comments on commit 1b50dbb

Please sign in to comment.