/** * Locale-aware routing helpers. * * SvelteKit's `resolve()` returns the language-neutral path a route lives at; * every link has to go through `localizeHref()` on top of that, or clicking it * would drop the visitor back into the base locale (the reroute hook in * `src/hooks.ts` strips the prefix again on the way in). */ import { goto } from '$app/navigation'; import { resolve } from '$app/paths'; import { type Locale, deLocalizeHref, locales, localizeHref } from '$lib/paraglide/runtime'; import type { PathnameWithSearchOrHash, RouteId, RouteIdWithSearchOrHash, RouteParams } from '$app/types'; // Mirrors SvelteKit's own (non-exported) argument type for `resolve`, so a // route id still demands exactly the params that route declares. type StripSearchOrHash = T extends `${infer P}?${string}` ? P : T extends `${infer P}#${string}` ? P : T; type ResolveArgs = T extends RouteId ? RouteParams extends Record ? [route: T] : [route: T, params: RouteParams] : StripSearchOrHash extends infer U extends RouteId ? RouteParams extends Record ? [route: T] : [route: T, params: RouteParams] : [route: T]; /** * `resolve()` for the current locale. Mirrors SvelteKit's own signature, so * route ids and their params keep being type-checked. */ export function href( ...args: ResolveArgs ): string { return localizeHref(resolve(...args)); } /** Same as `href`, but forced into a specific locale. */ export function hrefIn( locale: Locale, ...args: ResolveArgs ): string { return localizeHref(resolve(...args), { locale }); } /** `goto()` that keeps the visitor in their language. */ export function gotoLocalized( ...args: ResolveArgs ) { return goto(href(...args)); } /** * The language-neutral path for a URL that still carries its locale prefix - * what "which page am I on?" checks have to compare against. */ export function routePath(pathname: string): string { return deLocalizeHref(pathname); } /** Display names for the language switcher, in the language itself. */ export const LOCALE_LABELS: Record = { en: 'English', de: 'Deutsch', es: 'Español', fr: 'Français', it: 'Italiano' }; export const LOCALE_LIST = locales;