-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.server.ts
30 lines (29 loc) · 1.17 KB
/
hooks.server.ts
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
import { locales, setFetch } from '$lib/i18n'
import { createClient } from '$lib/i18n.server'
import type { Handle } from '@sveltejs/kit'
import { localeFlagsEngine, type Locale } from 'omni18n/ts'
export const handle: Handle = ({ event, resolve }) => {
//#region i18n
const preferredLocale = <Locale>event.cookies.get('language'),
usedLocales = <Locale[]>event.request.headers
.get('accept-language')
?.split(',')
.map((x) => x.split(' ')[0])
.map((x) => x && locales.find((locale) => locale.startsWith(x)))
.filter((x) => !!x) || []
if (preferredLocale) usedLocales.unshift(preferredLocale)
// Always have the en (original website) as feedback + avoid empty locales
usedLocales.push('en')
setFetch(event.fetch)
// Does not actually download anything, just centralizes so that if something is downloaded, it is done once
Object.assign(event.locals, {
i18nClient: createClient(usedLocales),
localeFlags: localeFlagsEngine(event.request.headers.get('user-agent'))
})
//#endregion
return resolve(event, {
transformPageChunk(opts: { html: string; done: boolean }) {
return opts.html.replaceAll('lang="%language%"', `lang="${usedLocales[0]}"`)
}
})
}