neuer checkout-pfad: /de/bestellen/paketID und /en/checkout/PackageID

This commit is contained in:
Codex Agent
2025-12-20 16:17:21 +01:00
parent 18297aa3f1
commit 6500b8df2c
18 changed files with 331 additions and 345 deletions

View File

@@ -18,6 +18,13 @@ describe('buildLocalizedPath', () => {
expect(buildLocalizedPath('/contact?ref=ad', 'de', supported)).toBe('/de/kontakt?ref=ad');
});
it('rewrites localized prefixes with dynamic segments', () => {
expect(buildLocalizedPath('/bestellen/123', 'en', supported, 'de', defaultLocaleRewrites))
.toBe('/en/checkout/123');
expect(buildLocalizedPath('/checkout/123', 'de', supported, 'de', defaultLocaleRewrites))
.toBe('/de/bestellen/123');
});
it('falls back to default locale when target not supported', () => {
expect(buildLocalizedPath('/demo', 'fr', supported)).toBe('/de/demo');
});

View File

@@ -6,6 +6,8 @@ export const defaultLocaleRewrites: LocaleRewriteMap = {
'/contact': { de: '/kontakt' },
'/so-funktionierts': { en: '/how-it-works' },
'/how-it-works': { de: '/so-funktionierts' },
'/bestellen': { en: '/checkout' },
'/checkout': { de: '/bestellen' },
'/anlaesse': { en: '/occasions' },
'/anlaesse/hochzeit': { en: '/occasions/wedding' },
'/anlaesse/geburtstag': { en: '/occasions/birthday' },
@@ -28,6 +30,34 @@ const sanitizePath = (input: string): string => {
return withoutTrailing;
};
const resolveRewrite = (
path: string,
targetLocale: string,
rewrites: LocaleRewriteMap,
): string => {
const exact = rewrites[path];
if (exact) {
return exact[targetLocale] ?? path;
}
const prefixKey = Object.keys(rewrites)
.filter((key) => key !== '/' && path.startsWith(`${key}/`))
.sort((a, b) => b.length - a.length)[0];
if (!prefixKey) {
return path;
}
const replacement = rewrites[prefixKey]?.[targetLocale];
if (!replacement) {
return path;
}
const suffix = path.slice(prefixKey.length);
return `${replacement}${suffix}`;
};
export const buildLocalizedPath = (
path: string | null | undefined,
targetLocale: string | undefined,
@@ -47,8 +77,7 @@ export const buildLocalizedPath = (
const trimmed = path.trim();
const [rawPath, rawQuery] = trimmed.split('?');
const normalizedPath = sanitizePath(rawPath);
const rewritesForPath = rewrites[normalizedPath] ?? {};
const rewrittenPath = rewritesForPath[nextLocale] ?? normalizedPath;
const rewrittenPath = resolveRewrite(normalizedPath, nextLocale, rewrites);
const base = rewrittenPath === '/' ? `/${nextLocale}` : `/${nextLocale}${rewrittenPath}`;
const sanitisedBase = base.replace(/\/{2,}/g, '/');
const query = rawQuery ? `?${rawQuery}` : '';

View File

@@ -429,7 +429,7 @@ function selectHighlightPackageId(packages: Package[]): number | null {
? isHighlightedPackage(selectedPackage, selectedVariant)
: false;
const purchaseUrl = selectedPackage ? `/purchase-wizard/${selectedPackage.id}` : '#';
const purchaseUrl = selectedPackage ? localizedPath(`/bestellen/${selectedPackage.id}`) : '#';
const { trackEvent } = useAnalytics();