17143 switcher fix..

This commit is contained in:
2025-06-17 11:40:34 +01:00
parent e2df26c935
commit 65f6b65613
+32 -22
View File
@@ -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 = () => { const handleLocaleSwitch = () => {
// Set cookie // const router = useRouter();
setCookie(null, "pedw_locale", switchLocale, { 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: "/", path: "/",
maxAge: 60 * 60 * 24 * 365, maxAge: 60 * 60 * 24 * 365, // 1 year
}); });
// Rebuild URL with query parameters // Navigate to new language path
const queryString = router.push(newPath);
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 });
}; };
return ( return (
@@ -170,7 +180,7 @@ const Header = (props) => {
</Link> </Link>
)} )}
<span {/* <span
onClick={handleLocaleSwitch} onClick={handleLocaleSwitch}
className="govuk-header__link" className="govuk-header__link"
lang={switchLocale} lang={switchLocale}
@@ -192,8 +202,8 @@ const Header = (props) => {
} }
> >
{t("common:available-in")} {t("common:available-in")}
</span> </span> */}
{/* <Link <Link
href={ href={
Object.keys(router.query).length > 0 Object.keys(router.query).length > 0
? router.pathname + ? router.pathname +
@@ -233,7 +243,7 @@ const Header = (props) => {
> >
{" "} {" "}
{t("common:available-in")} {t("common:available-in")}
</Link> */} </Link>
</div> </div>
</div> </div>
</div> </div>