Merge branch 'restray_i18n' of https://github.com/restray/coolify into restray-restray_i18n
This commit is contained in:
52
src/hooks.ts
52
src/hooks.ts
@@ -6,6 +6,7 @@ import { getUserDetails, sentry } from '$lib/common';
|
||||
import { version } from '$lib/common';
|
||||
import cookie from 'cookie';
|
||||
import { dev } from '$app/env';
|
||||
import { locales } from '$lib/translations';
|
||||
|
||||
const whiteLabeled = process.env['COOLIFY_WHITE_LABELED'] === 'true';
|
||||
const whiteLabelDetails = {
|
||||
@@ -20,6 +21,24 @@ export const handle = handleSession(
|
||||
},
|
||||
async function ({ event, resolve }) {
|
||||
let response;
|
||||
|
||||
const { url, request } = event;
|
||||
|
||||
// Get defined locales
|
||||
const supportedLocales = locales.get();
|
||||
let locale;
|
||||
|
||||
if (event.locals.cookies['lang']) {
|
||||
locale = event.locals.cookies['lang'];
|
||||
} else if (!locale) {
|
||||
locale = `${`${request.headers.get('accept-language')}`.match(
|
||||
/[a-zA-Z]+?(?=-|_|,|;)/
|
||||
)}`.toLowerCase();
|
||||
}
|
||||
|
||||
// Set default locale if user preferred locale does not match
|
||||
if (!supportedLocales.includes(locale)) locale = 'en';
|
||||
|
||||
try {
|
||||
if (event.locals.cookies) {
|
||||
if (event.locals.cookies['kit.session']) {
|
||||
@@ -29,7 +48,8 @@ export const handle = handleSession(
|
||||
teamId,
|
||||
permission,
|
||||
isAdmin: permission === 'admin' || permission === 'owner',
|
||||
expires: event.locals.session.data.expires
|
||||
expires: event.locals.session.data.expires,
|
||||
lang: locale
|
||||
};
|
||||
|
||||
if (JSON.stringify(event.locals.session.data) !== JSON.stringify(newSession)) {
|
||||
@@ -39,12 +59,14 @@ export const handle = handleSession(
|
||||
}
|
||||
|
||||
response = await resolve(event, {
|
||||
ssr: !event.url.pathname.startsWith('/webhooks/success')
|
||||
ssr: !event.url.pathname.startsWith('/webhooks/success'),
|
||||
transformPage: ({ html }) => html.replace(/<html.*>/, `<html lang="${locale}">`)
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
response = await resolve(event, {
|
||||
ssr: !event.url.pathname.startsWith('/webhooks/success')
|
||||
ssr: !event.url.pathname.startsWith('/webhooks/success'),
|
||||
transformPage: ({ html }) => html.replace(/<html.*>/, `<html lang="${locale}">`)
|
||||
});
|
||||
response.headers.append(
|
||||
'Set-Cookie',
|
||||
@@ -67,14 +89,34 @@ export const handle = handleSession(
|
||||
expires: new Date('Thu, 01 Jan 1970 00:00:01 GMT')
|
||||
})
|
||||
);
|
||||
} finally {
|
||||
return response;
|
||||
}
|
||||
|
||||
response.headers.append(
|
||||
'Set-Cookie',
|
||||
cookie.serialize('lang', locale, {
|
||||
path: '/',
|
||||
sameSite: 'strict',
|
||||
maxAge: 30 * 24 * 60 * 60
|
||||
})
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
);
|
||||
|
||||
export const getSession: GetSession = function ({ locals }) {
|
||||
// Get defined locales
|
||||
const supportedLocales = locales.get();
|
||||
let locale;
|
||||
|
||||
if (locals.cookies) {
|
||||
locale = supportedLocales.find(
|
||||
(l) => `${l}`.toLowerCase() === `${locals.cookies['lang']}`.toLowerCase()
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
lang: locale,
|
||||
version,
|
||||
whiteLabeled,
|
||||
whiteLabelDetails,
|
||||
|
Reference in New Issue
Block a user