--- 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; function translatePath(newLocale: string) { const segments = currentPath.split("/").filter(Boolean); 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) { const translations = routeTranslations[key as keyof typeof routeTranslations]; if (Object.values(translations).includes(segment)) { return translations[newLocale as keyof typeof translations] || segment; } } 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("/")}`; } const { locale } = Astro.params; ---