diff options
| author | Adam Soltys <[email protected]> | 2024-01-26 00:06:00 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-01-26 00:33:50 -0800 |
| commit | 3444b3c0fb349395a02d6db1eeaef57460b1a37f (patch) | |
| tree | af815eafd6c6075d49c8231efd955531376d993e /src/routes/+layout.server.js | |
| parent | 01fdad8ee4e6bb4213c0c81a8876cda67ad3085a (diff) | |
| download | coinos-ui-3444b3c0fb349395a02d6db1eeaef57460b1a37f.tar.gz coinos-ui-3444b3c0fb349395a02d6db1eeaef57460b1a37f.zip | |
update translations
Diffstat (limited to 'src/routes/+layout.server.js')
| -rw-r--r-- | src/routes/+layout.server.js | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/src/routes/+layout.server.js b/src/routes/+layout.server.js index 91d24d3d..5d1688fc 100644 --- a/src/routes/+layout.server.js +++ b/src/routes/+layout.server.js @@ -1,7 +1,13 @@ import { auth, get } from "$lib/utils"; -import { loadTranslations, locale } from "$lib/translations"; +import { + locales, + loadTranslations, + translations, + defaultLocale, +} from "$lib/translations"; -export let load = async ({ cookies, url }) => { +export let load = async ({ cookies, request, url }) => { + let { host, pathname } = url; let user; let token = cookies.get("token"); if (token) { @@ -10,8 +16,30 @@ export let load = async ({ cookies, url }) => { } catch (e) {} } - let { host, pathname } = url; - let locale = user?.language || "en"; - await loadTranslations(locale, pathname); - return { host, pathname }; + // 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 }, + translations: translations.get(), + }; }; |
