diff options
| author | Adam Soltys <[email protected]> | 2024-08-30 20:04:29 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-08-30 20:04:29 -0700 |
| commit | df71664f20312e81134ee90ad561f55d3020ee0a (patch) | |
| tree | 3986d1c5aac77f256ed1091290bc067c899e2196 /src/routes/+layout.server.ts | |
| parent | 20337319212896c484ac0f8d3dfbac3b8d26f089 (diff) | |
| download | coinos-ui-df71664f20312e81134ee90ad561f55d3020ee0a.tar.gz coinos-ui-df71664f20312e81134ee90ad561f55d3020ee0a.zip | |
typescript
Diffstat (limited to 'src/routes/+layout.server.ts')
| -rw-r--r-- | src/routes/+layout.server.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts new file mode 100644 index 00000000..aebac694 --- /dev/null +++ b/src/routes/+layout.server.ts @@ -0,0 +1,46 @@ +import { auth, get } from "$lib/utils"; +import { + locales, + loadTranslations, + translations, + defaultLocale, +} from "$lib/translations"; + +export let load = async ({ cookies, request, url }) => { + let { host, pathname } = url; + let user; + let token = cookies.get("token"); + if (token) { + try { + user = await get("/me", auth(cookies)); + } catch (e) {} + } + + // Try to get the locale from cookie + let locale = user?.language || (cookies.get("lang") || "").toLowerCase(); + + // Get user preferred locale + if (!locale) { + locale = `${`${request.headers.get("accept-language")}`.match( + /[a-zA-Z]+?(?=-|_|,|;)/ + )}`.toLowerCase(); + } + + // Get defined locales + let 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` + + return { + host, + pathname, + i18n: { locale, route: pathname }, + user, + translations: translations.get(), + }; +}; |
