-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
41 lines (33 loc) · 972 Bytes
/
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
import * as React from "react"
import { Link, navigate } from "gatsby"
import useLocales from "../hooks/useLocales"
import Layout from "../components/layout"
const IndexPage = () => {
const { locales } = useLocales()
React.useEffect(() => {
if (locales.length === 1) {
navigate(`/${locales[0].id}/`)
}
const preferredLocale = window.navigator.language
let matchingLocale = locales.find(
l => l.id.replace("_", "-") === preferredLocale
)
if (!matchingLocale)
matchingLocale = locales.find(
l => l.id === preferredLocale.replace(/-[A-Z]{2}/, "")
)
if (matchingLocale) {
navigate(`/${matchingLocale.id}/`)
}
}, [])
return (
<Layout style={{ justifyContent: 'space-around', textAlign: 'center' }}>
{locales.map(locale => (
<p key={locale.id}>
<Link to={`/${locale.id}/`}>{locale.viewIn}</Link>
</p>
))}
</Layout>
)
}
export default IndexPage