Revert "Patch bugs on locale redirections"
This reverts commit 614eb923d8
.
This commit is contained in:
parent
614eb923d8
commit
92d1f5aa55
66
src/hooks.ts
66
src/hooks.ts
@ -18,36 +18,6 @@ export const handle = handleSession(
|
||||
},
|
||||
async function ({ event, resolve }) {
|
||||
let response;
|
||||
let locale;
|
||||
|
||||
const { url, request } = event;
|
||||
const { pathname } = url;
|
||||
|
||||
// If this request is a route request
|
||||
if (routeRegex.test(pathname)) {
|
||||
// Get defined locales
|
||||
const supportedLocales = locales.get();
|
||||
|
||||
// Try to get locale from `pathname`.
|
||||
locale = supportedLocales.find(
|
||||
(l) => `${l}`.toLowerCase() === `${pathname.match(/[^/]+?(?=\/|$)/)}`.toLowerCase()
|
||||
);
|
||||
|
||||
if (!locale) {
|
||||
// Get user preferred locale
|
||||
locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z-]+?(?=_|,|;)/)}`;
|
||||
|
||||
// Set default locale if user preferred locale does not match
|
||||
if (!supportedLocales.includes(locale)) locale = 'en-US';
|
||||
|
||||
// 302 redirect
|
||||
return new Response(undefined, {
|
||||
headers: { location: `/${locale}${pathname}` },
|
||||
status: 302
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (event.locals.cookies) {
|
||||
if (event.locals.cookies['kit.session']) {
|
||||
@ -70,6 +40,7 @@ export const handle = handleSession(
|
||||
ssr: !event.url.pathname.startsWith('/webhooks/success')
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
response = await resolve(event, {
|
||||
ssr: !event.url.pathname.startsWith('/webhooks/success')
|
||||
});
|
||||
@ -96,9 +67,40 @@ export const handle = handleSession(
|
||||
);
|
||||
}
|
||||
|
||||
if (locale && response.headers.get('content-type') === 'text/html') {
|
||||
const { url, request } = event;
|
||||
const { pathname } = url;
|
||||
|
||||
// If this request is a route request
|
||||
if (routeRegex.test(pathname)) {
|
||||
// Get defined locales
|
||||
const supportedLocales = locales.get();
|
||||
|
||||
// Try to get locale from `pathname`.
|
||||
let locale = supportedLocales.find(
|
||||
(l) => `${l}`.toLowerCase() === `${pathname.match(/[^/]+?(?=\/|$)/)}`.toLowerCase()
|
||||
);
|
||||
|
||||
// If route locale is not supported
|
||||
if (!locale) {
|
||||
// Get user preferred locale
|
||||
locale = `${`${request.headers['accept-language']}`.match(
|
||||
/[a-zA-Z]+?(?=-|_|,|;)/
|
||||
)}`.toLowerCase();
|
||||
|
||||
// Set default locale if user preferred locale does not match
|
||||
if (!supportedLocales.includes(locale)) locale = 'en';
|
||||
|
||||
// 301 redirect
|
||||
return new Response(undefined, {
|
||||
headers: { location: `/${locale}${pathname}` },
|
||||
status: 301
|
||||
});
|
||||
}
|
||||
|
||||
// Add html `lang` attribute
|
||||
const body = await response.text();
|
||||
return new Response(body.replace(/<html.*>/, `<html lang="${locale}">`), response);
|
||||
|
||||
return new Response(`${body}`.replace(/<html.*>/, `<html lang="${locale}">`), response);
|
||||
}
|
||||
|
||||
return response;
|
||||
|
@ -1,4 +1,4 @@
|
||||
{
|
||||
"en-US": "English",
|
||||
"fr-FR": "Français"
|
||||
"en": "English",
|
||||
"fr": "Français"
|
||||
}
|
||||
|
@ -3,19 +3,19 @@ import lang from './lang.json';
|
||||
|
||||
/** @type {import('sveltekit-i18n').Config} */
|
||||
export const config = {
|
||||
fallbackLocale: 'en-US',
|
||||
fallbackLocale: 'en',
|
||||
translations: {
|
||||
'en-US': { lang },
|
||||
'fr-FR': { lang }
|
||||
en: { lang },
|
||||
fr: { lang }
|
||||
},
|
||||
loaders: [
|
||||
{
|
||||
locale: 'en-US',
|
||||
locale: 'en',
|
||||
key: '',
|
||||
loader: async () => (await import('../../static/locales/en.json')).default
|
||||
},
|
||||
{
|
||||
locale: 'fr-FR',
|
||||
locale: 'fr',
|
||||
key: '',
|
||||
loader: async () => (await import('../../static/locales/fr.json')).default
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<script context="module" lang="ts">
|
||||
import type { Load } from '@sveltejs/kit';
|
||||
import { isPublicPath } from '$lib/settings';
|
||||
import { loadTranslations } from '$lib/translations';
|
||||
import { locale, loadTranslations } from '$lib/translations';
|
||||
|
||||
export const load: Load = async ({ fetch, url, session }) => {
|
||||
const { pathname } = url;
|
||||
@ -44,7 +44,7 @@
|
||||
import { errorNotification } from '$lib/form';
|
||||
import { asyncSleep } from '$lib/components/common';
|
||||
import { del, get, post } from '$lib/api';
|
||||
import { t, loading } from '$lib/translations';
|
||||
import { t } from '$lib/translations';
|
||||
|
||||
let isUpdateAvailable = false;
|
||||
|
||||
@ -144,9 +144,7 @@
|
||||
</svelte:head>
|
||||
|
||||
<SvelteToast options={{ intro: { y: -64 }, duration: 3000, pausable: true }} />
|
||||
|
||||
{#if loading}
|
||||
{#if $session.userId}
|
||||
{#if $session.userId}
|
||||
<nav class="nav-main">
|
||||
<div class="flex h-screen w-full flex-col items-center transition-all duration-100">
|
||||
<div class="my-4 h-10 w-10"><img src="/favicon.png" alt="coolLabs logo" /></div>
|
||||
@ -534,10 +532,7 @@
|
||||
<option value={team.teamId}>{team.team.name} - {team.permission}</option>
|
||||
{/each}
|
||||
</select>
|
||||
{/if}
|
||||
<main>
|
||||
<slot />
|
||||
</main>
|
||||
{:else}
|
||||
<p>Loading...</p>
|
||||
{/if}
|
||||
<main>
|
||||
<slot />
|
||||
</main>
|
||||
|
Loading…
Reference in New Issue
Block a user