diff --git a/components/header.js b/components/header.js
index c5d7f902..939a3638 100644
--- a/components/header.js
+++ b/components/header.js
@@ -100,33 +100,47 @@ const Header = (props) => {
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 router = useRouter();
- const isWelsh = router.asPath.startsWith("/cy");
+ const isWelsh = router.locale === "cy";
+ const newLocale = isWelsh ? "en" : "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", {
+ // Set cookie first
+ setCookie(null, "pedw_locale", newLocale, {
path: "/",
maxAge: 60 * 60 * 24 * 365, // 1 year
});
- // Navigate to new language path
- router.push(newPath);
+ // Then navigate with the new locale
+ router.push(router.asPath, router.asPath, { locale: newLocale });
};
return (
@@ -180,7 +194,7 @@ const Header = (props) => {
)}
- {/* {
}
>
{t("common:available-in")}
- */}
-
+ {/* 0
? router.pathname +
@@ -243,7 +257,7 @@ const Header = (props) => {
>
{" "}
{t("common:available-in")}
-
+ */}
diff --git a/pages/myportal/index.js b/pages/myportal/index.js
index 43632ee0..276e4cd9 100644
--- a/pages/myportal/index.js
+++ b/pages/myportal/index.js
@@ -227,12 +227,15 @@ export const getServerSideProps = wrapper.getServerSideProps(
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) {
return {
redirect: {
- destination: `/${preferredLocale}/myportal`,
+ destination: `/${preferredLocale}${pathWithoutLocale}`,
permanent: false,
- locale: preferredLocale,
},
};
}