Revert "Add Locale URL"

This reverts commit d910b21185.
This commit is contained in:
Restray
2022-04-02 19:29:22 +02:00
parent 92d1f5aa55
commit 943300509b
187 changed files with 539 additions and 690 deletions

View File

@@ -6,9 +6,6 @@ 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 routeRegex = new RegExp(/^\/[^.]*([?#].*)?$/);
export const handle = handleSession(
{
@@ -65,45 +62,9 @@ export const handle = handleSession(
expires: new Date('Thu, 01 Jan 1970 00:00:01 GMT')
})
);
} finally {
return response;
}
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 response;
}
);