-
-
Notifications
You must be signed in to change notification settings - Fork 764
/
index.tsx
128 lines (117 loc) · 4.08 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import Link from 'next/link'
import { useRouter } from 'next/router'
import type { GetStaticProps, InferGetStaticPropsType } from 'next'
import { useTranslation, Trans } from 'next-i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { Header } from '../components/Header'
import { Footer } from '../components/Footer'
type Props = {
// Add custom props here
}
const Homepage = (
_props: InferGetStaticPropsType<typeof getStaticProps>
) => {
const router = useRouter()
const { t, i18n } = useTranslation('common')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const onToggleLanguageClick = (newLocale: string) => {
const { pathname, asPath, query } = router
router.push({ pathname, query }, asPath, { locale: newLocale })
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const clientSideLanguageChange = (newLocale: string) => {
i18n.changeLanguage(newLocale)
}
const changeTo = router.locale === 'en' ? 'de' : 'en'
// const changeTo = i18n.resolvedLanguage === 'en' ? 'de' : 'en'
return (
<>
<main>
<Header heading={t('h1')} title={t('title')} />
<div className="mainBox">
<div className="card">
<h3 className="card-title">
{t('blog.appDir.question')}
</h3>
<p className="card-text">
<Trans i18nKey="blog.appDir.answer">
Then check out
<a href={t('blog.appDir.link')}>this blog post</a>.
</Trans>
</p>
<a href={t('blog.appDir.link')}>
<img
className="card-img"
src="https://locize.com/blog/next-app-dir-i18n/next-app-dir-i18n.jpg"
/>
</a>
</div>
<div className="card">
<h3 className="card-title">
{t('blog.optimized.question')}
</h3>
<p className="card-text">
<Trans i18nKey="blog.optimized.answer">
Then you may have a look at
<a href={t('blog.optimized.link')}>this blog post</a>
.
</Trans>
</p>
<a href={t('blog.optimized.link')}>
<img
className="card-img"
src="https://locize.com/blog/next-i18next/next-i18next.jpg"
/>
</a>
</div>
<div className="card">
<h3 className="card-title">{t('blog.ssg.question')}</h3>
<p className="card-text">
<Trans i18nKey="blog.ssg.answer">
Then you may have a look at
<a href={t('blog.ssg.link')}>this blog post</a>.
</Trans>
</p>
<a href={t('blog.ssg.link')}>
<img
className="card-img"
src="https://locize.com/blog/next-i18n-static/title.jpg"
/>
</a>
</div>
</div>
<hr style={{ marginTop: 20, width: '90%' }} />
<div>
<Link href="/" locale={changeTo}>
<button>{t('change-locale', { changeTo })}</button>
</Link>
{/* alternative language change without using Link component
<button onClick={() => onToggleLanguageClick(changeTo)}>
{t('change-locale', { changeTo })}
</button>
*/}
{/* alternative language change without using Link component, but this will change language only on client side
<button onClick={() => clientSideLanguageChange(changeTo)}>
{t('change-locale', { changeTo })}
</button> */}
<Link href="/second-page">
<button type="button">{t('to-second-page')}</button>
</Link>
</div>
</main>
<Footer />
</>
)
}
// or getServerSideProps: GetServerSideProps<Props> = async ({ locale })
export const getStaticProps: GetStaticProps<Props> = async ({
locale,
}) => ({
props: {
...(await serverSideTranslations(locale ?? 'en', [
'common',
'footer',
])),
},
})
export default Homepage