codex has reworked checkout, but frontend doesnt work
This commit is contained in:
64
resources/js/hooks/useLocalizedRoutes.ts
Normal file
64
resources/js/hooks/useLocalizedRoutes.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { useMemo } from 'react';
|
||||
import { usePage } from '@inertiajs/react';
|
||||
|
||||
declare const route: ((name: string, params?: Record<string, unknown>, absolute?: boolean) => string) | undefined;
|
||||
|
||||
type PageProps = {
|
||||
locale?: string;
|
||||
url?: string;
|
||||
};
|
||||
|
||||
export const useLocalizedRoutes = () => {
|
||||
const page = usePage<PageProps>();
|
||||
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<string, unknown> = {}, 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]);
|
||||
};
|
||||
Reference in New Issue
Block a user