From 65f6b65613701ed700f65a5fa0b82335aa73a246 Mon Sep 17 00:00:00 2001 From: rdbsolutions Date: Tue, 17 Jun 2025 11:40:34 +0100 Subject: [PATCH] 17143 switcher fix.. --- components/header.js | 54 ++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/components/header.js b/components/header.js index ee9c4a6a..c5d7f902 100644 --- a/components/header.js +++ b/components/header.js @@ -94,29 +94,39 @@ const Header = (props) => { }); }; - const switchLocale = locale === "en" ? "cy" : "en"; + //const switchLocale = locale === "en" ? "cy" : "en"; + + const switchLocale = router.asPath.startsWith("/cy") ? "en" : "cy"; + + console.log("switch locale", locale === "en" ? "cy" : "en"); const handleLocaleSwitch = () => { - // Set cookie - setCookie(null, "pedw_locale", switchLocale, { + // const router = useRouter(); + const isWelsh = router.asPath.startsWith("/cy"); + + console.log( + isWelsh, + router.asPath, + router.basePath, + router.pathname, + router.route + ); + + // Switch path: remove /cy if switching to English, add /cy if switching to Welsh + const newPath = isWelsh + ? router.asPath.replace(/^\/cy/, "") || "/" + : router.asPath === "/" + ? "/cy" + : `/cy${router.asPath}`; + + // Persist user's choice in cookie + setCookie(null, "pedw_locale", isWelsh ? "en" : "cy", { path: "/", - maxAge: 60 * 60 * 24 * 365, + maxAge: 60 * 60 * 24 * 365, // 1 year }); - // Rebuild URL with query parameters - const queryString = - Object.keys(router.query).length > 0 - ? "?" + - Object.entries(router.query) - .map(([key, value]) => `${key}=${value}`) - .join("&") - : ""; - - // router.push(router.pathname + queryString, undefined, { - // locale: switchLocale, - // }); - - router.push(router.asPath, router.asPath, { locale: switchLocale }); + // Navigate to new language path + router.push(newPath); }; return ( @@ -170,7 +180,7 @@ const Header = (props) => { )} - { } > {t("common:available-in")} - - {/* */} + 0 ? router.pathname + @@ -233,7 +243,7 @@ const Header = (props) => { > {" "} {t("common:available-in")} - */} +