-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-ssr.js
39 lines (31 loc) · 1.11 KB
/
gatsby-ssr.js
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
import { LocalizationProvider } from '@fluent/react'
import { load } from 'cheerio'
import { getCurrentLangKey } from 'ptz-i18n'
import { createLocalization, useCurrentLocale, supportedLanguages } from '~/localization'
import localizationFiles from '~/localizations'
import wrapWithLibraries from './wrap-with-libraries'
const parseMarkup = (str) => {
const $ = load(str)
return $
.root()
.children()
.toArray()
.map(child => ({
nodeName: child.tagName,
textContent: $(child).text(),
}))
}
const localizations = Object.fromEntries(Object.entries(localizationFiles).map(([locale, ftl]) => [locale, createLocalization(locale, ftl, parseMarkup)]))
const PageWrapper = ({ children }) => {
const locale = useCurrentLocale(false)
return <LocalizationProvider l10n={localizations[locale]}>
{children}
</LocalizationProvider>
}
export const onRenderBody = ({ pathname, setHtmlAttributes }) => {
setHtmlAttributes({ lang: getCurrentLangKey(supportedLanguages, 'en', pathname) })
}
export const wrapPageElement = ({ element }) => <PageWrapper>
{element}
</PageWrapper>
export const wrapRootElement = wrapWithLibraries