2025-02-18 02:10:23 +00:00
|
|
|
"use client";
|
2024-11-26 03:09:48 +00:00
|
|
|
|
2025-02-18 02:10:23 +00:00
|
|
|
import { useLocale } from "next-intl";
|
2025-07-22 07:11:38 +00:00
|
|
|
import { useParams, useSearchParams } from "next/navigation";
|
2025-02-18 02:10:23 +00:00
|
|
|
import { locales } from "@/config";
|
|
|
|
|
import { usePathname, useRouter } from "@/i18n/routing";
|
2024-11-26 03:09:48 +00:00
|
|
|
|
2025-05-22 11:10:30 +00:00
|
|
|
import { useEffect, useState, useTransition } from "react";
|
2025-05-12 14:46:56 +00:00
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "@/components/ui/select";
|
2025-02-18 02:10:23 +00:00
|
|
|
import Image from "next/image";
|
2025-05-22 09:53:54 +00:00
|
|
|
import Cookies from "js-cookie";
|
2024-11-26 03:09:48 +00:00
|
|
|
|
2025-05-22 11:10:30 +00:00
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-26 03:09:48 +00:00
|
|
|
export default function LocalSwitcher() {
|
2025-02-18 02:10:23 +00:00
|
|
|
const [isPending, startTransition] = useTransition();
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const pathname = usePathname();
|
|
|
|
|
const params = useParams();
|
2025-05-22 09:53:54 +00:00
|
|
|
const localActive = useLocale() || "in";
|
2025-05-22 11:10:30 +00:00
|
|
|
const [selectedLang, setSelectedLang] = useState<string>("");
|
2025-07-22 07:11:38 +00:00
|
|
|
const searchParams = useSearchParams();
|
2025-05-22 09:53:54 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-05-22 11:10:30 +00:00
|
|
|
const storedLang = getLanguage();
|
2025-07-22 07:11:38 +00:00
|
|
|
let joinParam = "";
|
|
|
|
|
if (searchParams) {
|
|
|
|
|
joinParam = Array.from(searchParams.entries())
|
|
|
|
|
.map(([key, value]) => `${key}=${value}`)
|
|
|
|
|
.join("&");
|
|
|
|
|
}
|
2025-05-22 11:10:30 +00:00
|
|
|
|
2025-07-22 07:11:38 +00:00
|
|
|
if (pathname.includes("polda")) {
|
2025-05-22 09:53:54 +00:00
|
|
|
startTransition(() => {
|
2025-07-22 07:11:38 +00:00
|
|
|
router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, {
|
|
|
|
|
locale: "in",
|
|
|
|
|
});
|
2025-05-22 09:53:54 +00:00
|
|
|
});
|
2025-05-22 11:10:30 +00:00
|
|
|
} else {
|
2025-07-14 18:08:27 +00:00
|
|
|
if (!storedLang) {
|
|
|
|
|
setLanguage("in");
|
|
|
|
|
setSelectedLang("in");
|
|
|
|
|
|
|
|
|
|
startTransition(() => {
|
2025-07-22 07:11:38 +00:00
|
|
|
router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, {
|
|
|
|
|
locale: "in",
|
|
|
|
|
});
|
2025-07-14 18:08:27 +00:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
setSelectedLang(storedLang);
|
|
|
|
|
startTransition(() => {
|
2025-07-22 07:11:38 +00:00
|
|
|
router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, {
|
|
|
|
|
locale: storedLang,
|
|
|
|
|
});
|
2025-07-14 18:08:27 +00:00
|
|
|
});
|
|
|
|
|
}
|
2025-05-22 09:53:54 +00:00
|
|
|
}
|
2025-07-22 07:11:38 +00:00
|
|
|
}, [searchParams]);
|
2024-11-26 03:09:48 +00:00
|
|
|
|
2025-02-18 02:10:23 +00:00
|
|
|
const onSelectChange = (nextLocale: string) => {
|
2025-05-22 11:10:30 +00:00
|
|
|
setLanguage(nextLocale);
|
|
|
|
|
setSelectedLang(nextLocale);
|
2025-02-18 02:10:23 +00:00
|
|
|
startTransition(() => {
|
|
|
|
|
router.replace(pathname, { locale: nextLocale });
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
return (
|
2025-05-22 11:10:30 +00:00
|
|
|
<Select onValueChange={onSelectChange} value={selectedLang}>
|
2025-02-18 02:10:23 +00:00
|
|
|
<SelectTrigger className="w-[94px] border-none read-only:bg-transparent">
|
|
|
|
|
<SelectValue placeholder="Select a language" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
2025-05-12 14:46:56 +00:00
|
|
|
<SelectItem value="in" className="border-none">
|
2025-02-18 02:10:23 +00:00
|
|
|
<div className="flex items-center gap-1">
|
2025-05-12 14:46:56 +00:00
|
|
|
<Image
|
|
|
|
|
src="/images/all-img/flag-3.png"
|
|
|
|
|
alt="flag"
|
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
className="w-6 h-6 rounded-full"
|
|
|
|
|
/>
|
|
|
|
|
<span className="font-medium text-sm text-default-600 dark:text-default-700">
|
|
|
|
|
In
|
|
|
|
|
</span>
|
2025-02-18 02:10:23 +00:00
|
|
|
</div>
|
|
|
|
|
</SelectItem>
|
2025-05-12 14:46:56 +00:00
|
|
|
<SelectItem value="en" className="border-none">
|
2025-02-18 02:10:23 +00:00
|
|
|
<div className="flex items-center gap-1">
|
2025-05-12 14:46:56 +00:00
|
|
|
<Image
|
|
|
|
|
src="/images/all-img/flag-1.png"
|
|
|
|
|
alt="flag"
|
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
className="w-6 h-6 rounded-full"
|
|
|
|
|
/>
|
|
|
|
|
<span className="font-medium text-sm text-default-600 dark:text-default-700">
|
|
|
|
|
En
|
|
|
|
|
</span>
|
2025-02-18 02:10:23 +00:00
|
|
|
</div>
|
|
|
|
|
</SelectItem>
|
2025-05-12 14:46:56 +00:00
|
|
|
|
2025-02-18 02:10:23 +00:00
|
|
|
{/* <SelectItem value="ar">
|
2024-11-26 03:09:48 +00:00
|
|
|
<div className='flex items-center gap-1'>
|
|
|
|
|
<Image
|
|
|
|
|
src="/images/all-img/flag-2.png"
|
|
|
|
|
alt='flag'
|
|
|
|
|
width={24}
|
|
|
|
|
height={24}
|
|
|
|
|
className='w-6 h-6 rounded-full'
|
|
|
|
|
/>
|
|
|
|
|
<span className='font-medium text-sm text-default-600 dark:text-default-700'>Ar</span>
|
|
|
|
|
</div>
|
2024-12-07 16:32:39 +00:00
|
|
|
</SelectItem> */}
|
2025-02-18 02:10:23 +00:00
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
);
|
|
|
|
|
}
|