diff --git a/components/partials/header/locale-switcher.tsx b/components/partials/header/locale-switcher.tsx index fcb2582b..8b295575 100644 --- a/components/partials/header/locale-switcher.tsx +++ b/components/partials/header/locale-switcher.tsx @@ -5,7 +5,7 @@ import { useParams } from "next/navigation"; import { locales } from "@/config"; import { usePathname, useRouter } from "@/i18n/routing"; -import { useTransition } from "react"; +import { useEffect, useTransition } from "react"; import { Select, SelectContent, @@ -14,18 +14,32 @@ import { SelectValue, } from "@/components/ui/select"; import Image from "next/image"; +import Cookies from "js-cookie"; export default function LocalSwitcher() { const [isPending, startTransition] = useTransition(); const router = useRouter(); const pathname = usePathname(); const params = useParams(); - const localActive = useLocale(); + const localActive = useLocale() || "in"; + const defaultLocale = Cookies.get("locale"); + + useEffect(() => { + console.log("locale", defaultLocale); + + if (!defaultLocale) { + Cookies.set("locale", "in"); + startTransition(() => { + router.replace(pathname, { locale: "in" }); + }); + } + }, []); const onSelectChange = (nextLocale: string) => { startTransition(() => { router.replace(pathname, { locale: nextLocale }); }); + Cookies.set("locale", nextLocale); }; return (