From d1c619bb9e0704a6c749fc5df55724a38cc25c38 Mon Sep 17 00:00:00 2001 From: Emil Gulamov <125820963+mearashadowfax@users.noreply.github.com> Date: Sun, 8 Sep 2024 14:53:53 +0400 Subject: [PATCH] Refactor language switching logic for blog posts, insights and products - Ensure URLs maintain correct structure without duplicating language prefixes - Update script to dynamically reconstruct URLs when switching languages - Improve logic for adding and removing language prefixes based on current path --- src/components/ui/LanguagePicker.astro | 24 +++++------ src/components/ui/cards/CardBlog.astro | 4 +- src/components/ui/cards/CardBlogRecent.astro | 4 +- src/components/ui/cards/CardInsight.astro | 6 ++- src/components/ui/cards/CardRelated.astro | 4 +- src/components/ui/cards/CardSmall.astro | 4 +- src/components/ui/cards/CardWide.astro | 4 +- src/pages/blog/[...slug].astro | 29 +++++++------ src/pages/fr/blog/[...slug].astro | 44 ++++++++++---------- src/pages/fr/blog/index.astro | 2 +- src/pages/fr/insights/[...slug].astro | 13 +++--- src/pages/fr/products/[...slug].astro | 16 ++++--- src/pages/insights/[...slug].astro | 15 ++++--- src/pages/products/[...slug].astro | 16 ++++--- 14 files changed, 104 insertions(+), 81 deletions(-) diff --git a/src/components/ui/LanguagePicker.astro b/src/components/ui/LanguagePicker.astro index ed22dd0..4454b04 100644 --- a/src/components/ui/LanguagePicker.astro +++ b/src/components/ui/LanguagePicker.astro @@ -74,20 +74,18 @@ import Icon from "./icons/Icon.astro"; // Disable the selection of the same language if (lang === url.pathname.split("/")[1]) return; - if ( - url.pathname.includes("/post") || - url.pathname.includes("/insight") || - url.pathname.includes("/item") - ) { - if (url.pathname.includes("en")) { - pathParts.unshift(lang); - pathParts.splice(2, 0, lang); - } else { - pathParts.unshift(lang); - pathParts.splice(2, 0, "en"); - } - } else { + // Determine if the current URL already has a language prefix + const currentLang = languages.includes(pathParts[0] as TLanguage) ? pathParts[0] : "en"; + + // Remove current language prefix from pathParts + if (languages.includes(pathParts[0] as TLanguage)) { + pathParts.shift(); + } + + // Determine if we are switching to a different language + if (lang !== currentLang) { if (lang !== "en") { + // Add the new language prefix for non-English pathParts.unshift(lang); } } diff --git a/src/components/ui/cards/CardBlog.astro b/src/components/ui/cards/CardBlog.astro index 3d6439d..6bde4ae 100644 --- a/src/components/ui/cards/CardBlog.astro +++ b/src/components/ui/cards/CardBlog.astro @@ -19,7 +19,9 @@ interface Props { which automatically prefetches the linked page to speed up navigation. --> diff --git a/src/components/ui/cards/CardBlogRecent.astro b/src/components/ui/cards/CardBlogRecent.astro index ce6f573..6f9f0f3 100644 --- a/src/components/ui/cards/CardBlogRecent.astro +++ b/src/components/ui/cards/CardBlogRecent.astro @@ -36,7 +36,7 @@ interface Props { > {blogEntry.data.description} @@ -57,7 +57,7 @@ interface Props {