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 }) {
|
async function ({ event, resolve }) {
|
||||||
let response;
|
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 {
|
try {
|
||||||
if (event.locals.cookies) {
|
if (event.locals.cookies) {
|
||||||
if (event.locals.cookies['kit.session']) {
|
if (event.locals.cookies['kit.session']) {
|
||||||
@ -70,6 +40,7 @@ export const handle = handleSession(
|
|||||||
ssr: !event.url.pathname.startsWith('/webhooks/success')
|
ssr: !event.url.pathname.startsWith('/webhooks/success')
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
response = await resolve(event, {
|
response = await resolve(event, {
|
||||||
ssr: !event.url.pathname.startsWith('/webhooks/success')
|
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();
|
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;
|
return response;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"en-US": "English",
|
"en": "English",
|
||||||
"fr-FR": "Français"
|
"fr": "Français"
|
||||||
}
|
}
|
||||||
|
@ -3,19 +3,19 @@ import lang from './lang.json';
|
|||||||
|
|
||||||
/** @type {import('sveltekit-i18n').Config} */
|
/** @type {import('sveltekit-i18n').Config} */
|
||||||
export const config = {
|
export const config = {
|
||||||
fallbackLocale: 'en-US',
|
fallbackLocale: 'en',
|
||||||
translations: {
|
translations: {
|
||||||
'en-US': { lang },
|
en: { lang },
|
||||||
'fr-FR': { lang }
|
fr: { lang }
|
||||||
},
|
},
|
||||||
loaders: [
|
loaders: [
|
||||||
{
|
{
|
||||||
locale: 'en-US',
|
locale: 'en',
|
||||||
key: '',
|
key: '',
|
||||||
loader: async () => (await import('../../static/locales/en.json')).default
|
loader: async () => (await import('../../static/locales/en.json')).default
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
locale: 'fr-FR',
|
locale: 'fr',
|
||||||
key: '',
|
key: '',
|
||||||
loader: async () => (await import('../../static/locales/fr.json')).default
|
loader: async () => (await import('../../static/locales/fr.json')).default
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<script context="module" lang="ts">
|
<script context="module" lang="ts">
|
||||||
import type { Load } from '@sveltejs/kit';
|
import type { Load } from '@sveltejs/kit';
|
||||||
import { isPublicPath } from '$lib/settings';
|
import { isPublicPath } from '$lib/settings';
|
||||||
import { loadTranslations } from '$lib/translations';
|
import { locale, loadTranslations } from '$lib/translations';
|
||||||
|
|
||||||
export const load: Load = async ({ fetch, url, session }) => {
|
export const load: Load = async ({ fetch, url, session }) => {
|
||||||
const { pathname } = url;
|
const { pathname } = url;
|
||||||
@ -44,7 +44,7 @@
|
|||||||
import { errorNotification } from '$lib/form';
|
import { errorNotification } from '$lib/form';
|
||||||
import { asyncSleep } from '$lib/components/common';
|
import { asyncSleep } from '$lib/components/common';
|
||||||
import { del, get, post } from '$lib/api';
|
import { del, get, post } from '$lib/api';
|
||||||
import { t, loading } from '$lib/translations';
|
import { t } from '$lib/translations';
|
||||||
|
|
||||||
let isUpdateAvailable = false;
|
let isUpdateAvailable = false;
|
||||||
|
|
||||||
@ -144,8 +144,6 @@
|
|||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<SvelteToast options={{ intro: { y: -64 }, duration: 3000, pausable: true }} />
|
<SvelteToast options={{ intro: { y: -64 }, duration: 3000, pausable: true }} />
|
||||||
|
|
||||||
{#if loading}
|
|
||||||
{#if $session.userId}
|
{#if $session.userId}
|
||||||
<nav class="nav-main">
|
<nav class="nav-main">
|
||||||
<div class="flex h-screen w-full flex-col items-center transition-all duration-100">
|
<div class="flex h-screen w-full flex-col items-center transition-all duration-100">
|
||||||
@ -538,6 +536,3 @@
|
|||||||
<main>
|
<main>
|
||||||
<slot />
|
<slot />
|
||||||
</main>
|
</main>
|
||||||
{:else}
|
|
||||||
<p>Loading...</p>
|
|
||||||
{/if}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user