From 1f79c6594d34c19dbf6052df64d5a55be7e9e2f6 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 29 Apr 2023 23:44:29 -0700 Subject: get user locale from db --- src/routes/+layout.js | 8 -------- src/routes/+layout.server.js | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) delete mode 100644 src/routes/+layout.js create mode 100644 src/routes/+layout.server.js (limited to 'src/routes') diff --git a/src/routes/+layout.js b/src/routes/+layout.js deleted file mode 100644 index add74f87..00000000 --- a/src/routes/+layout.js +++ /dev/null @@ -1,8 +0,0 @@ -import { loadTranslations, locale } from '$lib/translations'; -export const load = async ({ url }) => { - const { pathname } = url; - const defaultLocale = 'en'; // get from cookie, user session, ... - const initLocale = locale.get() || defaultLocale; // set default if no locale already set - await loadTranslations(initLocale, pathname); // keep this just before the `return` - return {}; -}; diff --git a/src/routes/+layout.server.js b/src/routes/+layout.server.js new file mode 100644 index 00000000..5359d022 --- /dev/null +++ b/src/routes/+layout.server.js @@ -0,0 +1,17 @@ +import { auth, get } from "$lib/utils"; +import { loadTranslations, locale } from '$lib/translations'; +export const load = async ({ cookies, url }) => { + let user; + let token = cookies.get('token'); + if (token) { + try { + user = await get('/me', auth(cookies)); + } catch (e) {} + } + + const { pathname } = url; + const defaultLocale = user?.language || 'en'; // get from cookie, user session, ... + const initLocale = locale.get() || defaultLocale; // set default if no locale already set + await loadTranslations(initLocale, pathname); // keep this just before the `return` + return {}; +}; -- cgit v1.2.3