"use client"; import { useLocale } from "next-intl"; import { useParams } 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(""); useEffect(() => { const storedLang = getLanguage(); if (!storedLang) { setLanguage("in"); setSelectedLang("in"); startTransition(() => { router.replace(pathname, { locale: "in" }); }); } else { setSelectedLang(storedLang); startTransition(() => { router.replace(pathname, { locale: storedLang }); }); } }, []); const onSelectChange = (nextLocale: string) => { setLanguage(nextLocale); setSelectedLang(nextLocale); startTransition(() => { router.replace(pathname, { locale: nextLocale }); }); }; return ( ); }