init locale in

This commit is contained in:
Rama Priyanto 2025-07-24 11:26:45 +07:00
parent 8645f913bf
commit cfd89ea3f8
2 changed files with 94 additions and 18031 deletions

View File

@ -31,45 +31,43 @@ export default function LocalSwitcher() {
const router = useRouter(); const router = useRouter();
const pathname = usePathname(); const pathname = usePathname();
const params = useParams(); const params = useParams();
const localActive = useLocale() || "in"; const locale = useLocale() || "in";
const [selectedLang, setSelectedLang] = useState<string>(""); const [selectedLang, setSelectedLang] = useState<string>("in");
const searchParams = useSearchParams(); const searchParams = useSearchParams();
const [hasInitialized, setHasInitialized] = useState(false);
useEffect(() => { useEffect(() => {
const storedLang = getLanguage(); const storedLang = getLanguage();
let joinParam = ""; let joinParam = "";
if (searchParams) { if (searchParams) {
joinParam = Array.from(searchParams.entries()) joinParam = Array.from(searchParams.entries())
.map(([key, value]) => `${key}=${value}`) .map(([key, value]) => `${key}=${value}`)
.join("&"); .join("&");
} }
if (pathname.includes("polda")) { const redirectTo = (lang: string) => {
startTransition(() => { startTransition(() => {
router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, { router.replace(pathname + (joinParam ? `?${joinParam}` : ""), {
locale: "in", locale: lang,
}); });
}); });
} else { };
if (!hasInitialized) {
if (!storedLang) { if (!storedLang) {
setLanguage("in"); setLanguage("in");
setSelectedLang("in"); setSelectedLang("in");
redirectTo("in");
startTransition(() => {
router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, {
locale: "in",
});
});
} else { } else {
setSelectedLang(storedLang); setSelectedLang(storedLang);
startTransition(() => { if (locale !== storedLang) {
router.replace(pathname + joinParam === "" ? "" : `?${joinParam}`, { redirectTo(storedLang);
locale: storedLang, }
});
});
} }
setHasInitialized(true);
} }
}, [searchParams]); }, []);
const onSelectChange = (nextLocale: string) => { const onSelectChange = (nextLocale: string) => {
setLanguage(nextLocale); setLanguage(nextLocale);

18091
package-lock.json generated

File diff suppressed because it is too large Load Diff