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")}
- */}
+