--- import type { CollectionEntry } from 'astro:content'; import { DEFAULT_LOCALE, type Locale, SITE } from '~/consts'; import { aboutSegment, categoryIndexSegment, entryHref, findTagBySlug, findTranslation, tagHref, tagIndexSegment, tagSegment, } from '~/i18n/posts'; import { getLocaleFromUrl, localizePath, switchLocalePath, t } from '~/i18n/ui'; import HeaderLink from '~/components/HeaderLink.astro'; interface Props { locale?: Locale; /** The current page's content entry, if any — used to resolve a translated language-switch link. */ entry?: CollectionEntry<'posts' | 'categories'>; } const { entry } = Astro.props; const locale: Locale = Astro.props.locale ?? getLocaleFromUrl(Astro.url); const otherLocale: Locale = locale === 'de' ? 'en' : 'de'; const homeHref = localizePath('/', locale); const aboutHref = localizePath(`/${aboutSegment(locale)}`, locale); const categoriesHref = localizePath(`/${categoryIndexSegment(locale)}`, locale); const tagsHref = localizePath(`/${tagIndexSegment(locale)}`, locale); const translated = entry ? await findTranslation(entry, otherLocale) : undefined; // If we're on a tag detail page, verify the tag exists in the target locale — // otherwise the switched URL would 404. Fall back to the target tags index. async function resolveSwitchHref(): Promise { if (translated) return entryHref(translated); if (entry) return localizePath('/', otherLocale); const parts = Astro.url.pathname.split('/').filter(Boolean); const localePrefix = parts[0] === locale ? 1 : 0; const first = parts[localePrefix]; const slug = parts[localePrefix + 1]; if (first === tagSegment(locale) && slug) { const target = await findTagBySlug(otherLocale, slug); return target ? tagHref(otherLocale, target) : localizePath(`/${tagIndexSegment(otherLocale)}`, otherLocale); } return switchLocalePath(Astro.url.pathname, otherLocale); } const switchHref = await resolveSwitchHref(); ---