zonemaster-gui/src/middleware.ts
Tobias Bleckert bc332a8683 Handle subdirectories when parsing language in middleware
Co-authored-by: Marc van der Wal <103426270+marc-vanderwal@users.noreply.github.com>
2025-12-15 21:49:27 +01:00

22 lines
722 B
TypeScript

import { defineMiddleware } from 'astro:middleware';
import { setLocale, isValidLocale } from '@/messages';
export const onRequest = defineMiddleware(({ url }, next) => {
// base should always end with '/'
const base = import.meta.env.BASE_URL;
let pathname = url.pathname;
if (pathname.startsWith(base)) {
pathname = pathname.slice(base.length);
}
// Extract locale from the first path segment after removing base URL
// (e.g., /subdirectory/en/page -> en/page -> 'en')
const pathSegments = pathname.split('/').filter(Boolean);
const localeFromPath = pathSegments[0];
if (isValidLocale(localeFromPath)) {
setLocale(localeFromPath);
}
return next();
});