"use client"; import { useLocale } from "next-intl"; import { useParams, useSearchParams } from "next/navigation"; import { locales } from "@/config"; import { usePathname, useRouter } from "@/i18n/routing"; import { useEffect, useState, useTransition } from "react"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import Image from "next/image"; import Cookies from "js-cookie"; export const getLanguage = (): string | null => { if (typeof window === "undefined") return null; return localStorage.getItem("language"); }; export const setLanguage = (value: string) => { if (typeof window === "undefined") return; localStorage.setItem("language", value); }; export default function LocalSwitcher() { const [isPending, startTransition] = useTransition(); const router = useRouter(); const pathname = usePathname(); const params = useParams(); const localActive = useLocale() || "in"; const [selectedLang, setSelectedLang] = useState(""); const searchParams = useSearchParams(); useEffect(() => { const storedLang = getLanguage(); let joinParam = ""; if (searchParams) { joinParam = Array.from(searchParams.entries()) .map(([key, value]) => `${key}=${value}`) .join("&"); } if (pathname.includes("polda")) { startTransition(() => { router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, { locale: "in", }); }); } else { if (!storedLang) { setLanguage("in"); setSelectedLang("in"); startTransition(() => { router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, { locale: "in", }); }); } else { setSelectedLang(storedLang); startTransition(() => { router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, { locale: storedLang, }); }); } } }, [searchParams]); const onSelectChange = (nextLocale: string) => { setLanguage(nextLocale); setSelectedLang(nextLocale); startTransition(() => { router.replace(pathname, { locale: nextLocale }); }); }; return ( ); }