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 = () => {
// 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) => {
</Link>
)}
<span
{/* <span
onClick={handleLocaleSwitch}
className="govuk-header__link"
lang={switchLocale}
@@ -192,8 +202,8 @@ const Header = (props) => {
}
>
{t("common:available-in")}
</span>
{/* <Link
</span> */}
<Link
href={
Object.keys(router.query).length > 0
? router.pathname +
@@ -233,7 +243,7 @@ const Header = (props) => {
>
{" "}
{t("common:available-in")}
</Link> */}
</Link>
</div>
</div>
</div>