import { defaultLocale } from '@/i18n'; /** * URL prefix for a locale under the "as-needed" strategy: none for the default * locale (clean URLs), `/{locale}` for every other one. * * ⛔ The default locale arrives as a real `params.locale` too. The middleware * rewrites `/foo` to `/{defaultLocale}/foo` internally and 308-redirects * `/{defaultLocale}/foo` back to `/foo`, so a canonical tag, breadcrumb link or * JSON-LD url built as `/${locale}/foo` for the default locale points at a URL * that redirects. Build every public path through this helper. * * Reads the same scaffold-time `defaultLocale` the middleware uses, so the two * can never disagree about which locale is the unprefixed one. */ export function localePrefix(locale: string | undefined | null): string { return locale && locale !== defaultLocale ? `/${locale}` : ''; } /** * Site-relative public path for `path` in `locale`: * `localePath('he', '/blog')` → `/he/blog`, default locale → `/blog`, * `localePath('he', '/')` → `/he`, `localePath(undefined, '/')` → `/`. */ export function localePath(locale: string | undefined | null, path: string): string { const prefix = localePrefix(locale); if (path === '' || path === '/') return prefix || '/'; return `${prefix}${path.startsWith('/') ? path : `/${path}`}`; }