17143 adjsut switcher
This commit is contained in:
+39
-25
@@ -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>
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user