import { useMemo } from 'react'; import { usePage } from '@inertiajs/react'; declare const route: ((name: string, params?: Record, absolute?: boolean) => string) | undefined; type PageProps = { locale?: string; url?: string; }; export const useLocalizedRoutes = () => { const page = usePage(); const currentUrl = page.url ?? (typeof window !== 'undefined' ? window.location.pathname : '/') ?? '/'; return useMemo(() => { let locale = page.props.locale; if (!locale) { if (currentUrl.startsWith('/en')) { locale = 'en'; } else if (currentUrl.startsWith('/de')) { locale = 'de'; } } if (!locale) { locale = 'de'; } const localePrefix = locale ? `/${locale}` : ''; const localizedPath = (path = '/') => { if (!path || path === '/') { return localePrefix || '/'; } const normalized = path.startsWith('/') ? path : `/${path}`; const result = `${localePrefix}${normalized}`; return result.replace(/\/+$/, '').replace(/\/+/g, '/'); }; const localizedRoute = (name: string, params: Record = {}, absolute = false) => { if (typeof route === 'function') { const payload = locale ? { locale, ...params } : params; try { return route(name, payload, absolute); } catch (error) { console.warn('Failed to resolve route', name, error); } } return localizedPath(name.startsWith('/') ? name : `/${name}`); }; return { locale, localePrefix, localizedPath, localizedRoute, }; }, [page.props.locale, currentUrl]); };