--- 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 allEditorial = await getCollection("editorial"); const tl = createTranslator(Astro.currentLocale); const currentLocale = Astro.currentLocale; const currentPath = Astro.url.pathname; const { locale } = Astro.params; const languages = [ { code: "es", icon: "icon_flag_es", label: "Español" }, { code: "en", icon: "icon_flag_uk", label: "English" }, { code: "he", icon: "flagpack--il", label: "עברית" }, { code: "pt", icon: "flagpack--br", label: "Português" }, { code: "fr", icon: "flagpack--fr", label: "Français" }, { code: "ru", icon: "flagpack--ru", label: "Русский" }, { code: "rw", icon: "flagpack--rw", label: "Kinyarwanda" }, { code: "kr", icon: null, label: "Kreole" }, ]; const navItems = [ { href: "#somos", key: "nav.about" }, { href: "#programs", key: "nav.programs" }, { href: "#news", key: "nav.news" }, { href: "#editorial", key: "nav.editorial" }, ]; const sidebarNavItems = [ { href: `/`, key: "nav.home" }, { href: "#somos", key: "nav.about" }, { href: "#programs", key: "nav.programs" }, { href: "#news", key: "nav.news" }, { href: "#editorial", key: "nav.editorial" }, ]; function translatePath(newLocale: string) { const segments = currentPath.split("/").filter(Boolean); if (segments.length === 0) return `/${newLocale}`; const remainingSegments = segments.slice(1); const allRouteNames = Object.values(routeTranslations).flatMap(r => Object.values(r)); 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; }); if (segments.length >= 2 && allRouteNames.includes(segments[1])) { if (segments.length >= 3) { const currentId = segments[segments.length - 1]; const baseId = currentId.split("/").pop(); const existsInNews = allNews.some( (post) => post.id.endsWith(baseId!) && post.data.locale === newLocale, ); const existsInEditorial = allEditorial.some( (post) => post.id.endsWith(baseId!) && post.data.locale === newLocale, ); if (!existsInNews && !existsInEditorial) return `/${newLocale}/${translatedSegments[0]}`; return `/${newLocale}/${translatedSegments[0]}/${newLocale}/${baseId}`; } } return `/${[newLocale, ...translatedSegments].join("/")}`; } ---