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

export const load = async ({ cookies, request }) => {
  // 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, "/");

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

  return {
    theme,
    i18n: { locale, route: "/" },
  };
};