From 97b77f99047a58e33d3ddfe3c494655ed332d07f Mon Sep 17 00:00:00 2001 From: Esteban Date: Fri, 8 May 2026 15:16:45 -0500 Subject: [PATCH] feat: add Header component with responsive navigation and i18n path switching logic --- src/components/Header.astro | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/components/Header.astro b/src/components/Header.astro index 3ca755d..c5aa10f 100644 --- a/src/components/Header.astro +++ b/src/components/Header.astro @@ -2,7 +2,11 @@ import Button from "./ui/Button.astro"; import { Icon } from "astro-icon/components"; +import { getCollection } from "astro:content"; import { createTranslator, routeTranslations } from "../i18n/index.ts"; + +const allNews = await getCollection("news"); + const tl = createTranslator(Astro.currentLocale); const currentLocale = Astro.currentLocale; const currentPath = Astro.url.pathname; @@ -13,6 +17,7 @@ function translatePath(newLocale: string) { if (segments.length === 0) return `/${newLocale}`; const remainingSegments = segments.slice(1); + const newsRouteNames = Object.values(routeTranslations.news); const translatedSegments = remainingSegments.map(segment => { for (const key in routeTranslations) { @@ -24,6 +29,29 @@ function translatePath(newLocale: string) { return segment; }); + // Lógica para noticias + if (segments.length >= 2 && newsRouteNames.includes(segments[1])) { + const isDetail = segments.length >= 3; + + if (isDetail) { + const currentId = segments[segments.length - 1]; + const baseId = currentId.split('/').pop(); + + const exists = allNews.some(post => + post.id.endsWith(baseId!) && post.data.locale === newLocale + ); + + if (!exists) { + // Redirigir al home de noticias si no existe la traducción + return `/${newLocale}/${translatedSegments[0]}`; + } + + // Reconstruir ID con el nuevo locale + const newId = `${newLocale}/${baseId}`; + return `/${newLocale}/${translatedSegments[0]}/${newId}`; + } + } + return `/${[newLocale, ...translatedSegments].join('/')}`; }