aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/+layout.server.ts
blob: 61c46ea6777ba321777edb08965969b273513a42 (plain) (blame)
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
import { defaultLocale, loadTranslations, locales } from "$lib/translations/index";

export const load = async ({ cookies, request, url }) => {
  const { pathname } = url;

  // Try to get the locale from cookie
  let locale = (cookies.get("lang") || "").toLowerCase();

  // Get user preferred locale
  if (!locale) {
    locale = `${`${request.headers.get("accept-language")}`.match(
      /[a-zA-Z]+?(?=-|_|,|;)/,
    )}`.toLowerCase();
  }

  // Get defined locales
  const supportedLocales = locales.get().map((l) => l.toLowerCase());

  // Use default locale if current locale is not supported
  if (!supportedLocales.includes(locale)) {
    locale = defaultLocale;
  }

  await loadTranslations(locale, pathname); // keep this just before the `return`

  const theme = cookies.get("theme") || "light";

  return {
    theme,
    pathname,
    i18n: { locale, route: pathname },
  };
};