17143 adjsut switcher

This commit is contained in:
2025-06-18 10:17:26 +01:00
parent 56ad0c4843
commit 412f7d2e90
2 changed files with 44 additions and 27 deletions
+39 -25
View File
@@ -100,33 +100,47 @@ const Header = (props) => {
console.log("switch locale", locale === "en" ? "cy" : "en"); console.log("switch locale", locale === "en" ? "cy" : "en");
// const handleLocaleSwitch = () => {
// // 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, // 1 year
// });
// // Navigate to new language path
// router.push(newPath);
// };
const handleLocaleSwitch = () => { const handleLocaleSwitch = () => {
// const router = useRouter(); const isWelsh = router.locale === "cy";
const isWelsh = router.asPath.startsWith("/cy"); const newLocale = isWelsh ? "en" : "cy";
console.log( // Set cookie first
isWelsh, setCookie(null, "pedw_locale", newLocale, {
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, // 1 year maxAge: 60 * 60 * 24 * 365, // 1 year
}); });
// Navigate to new language path // Then navigate with the new locale
router.push(newPath); router.push(router.asPath, router.asPath, { locale: newLocale });
}; };
return ( return (
@@ -180,7 +194,7 @@ const Header = (props) => {
</Link> </Link>
)} )}
{/* <span <span
onClick={handleLocaleSwitch} onClick={handleLocaleSwitch}
className="govuk-header__link" className="govuk-header__link"
lang={switchLocale} lang={switchLocale}
@@ -202,8 +216,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 +
@@ -243,7 +257,7 @@ const Header = (props) => {
> >
{" "} {" "}
{t("common:available-in")} {t("common:available-in")}
</Link> </Link> */}
</div> </div>
</div> </div>
</div> </div>
+5 -2
View File
@@ -227,12 +227,15 @@ export const getServerSideProps = wrapper.getServerSideProps(
preferredLocale = cookies.pedw_locale; preferredLocale = cookies.pedw_locale;
} }
// Strip the current locale from the URL path
const pathWithoutLocale = ctx.resolvedUrl.replace(/^\/(cy|en)/, "");
//console.log("==============================", pathWithoutLocale);
if (preferredLocale !== locale) { if (preferredLocale !== locale) {
return { return {
redirect: { redirect: {
destination: `/${preferredLocale}/myportal`, destination: `/${preferredLocale}${pathWithoutLocale}`,
permanent: false, permanent: false,
locale: preferredLocale,
}, },
}; };
} }