Merged PR 1686: 17143 adjsut switcher

17143 adjsut switcher

Related work items: #17143
This commit is contained in:
Robert Bond
2025-06-18 09:18:21 +00:00
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");
// 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) => {
</Link>
)}
{/* <span
<span
onClick={handleLocaleSwitch}
className="govuk-header__link"
lang={switchLocale}
@@ -202,8 +216,8 @@ const Header = (props) => {
}
>
{t("common:available-in")}
</span> */}
<Link
</span>
{/* <Link
href={
Object.keys(router.query).length > 0
? router.pathname +
@@ -243,7 +257,7 @@ const Header = (props) => {
>
{" "}
{t("common:available-in")}
</Link>
</Link> */}
</div>
</div>
</div>
+5 -2
View File
@@ -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,
},
};
}