From 4382b55196991f9a248d4a276bac8332a485d381 Mon Sep 17 00:00:00 2001 From: Rama Priyanto Date: Thu, 22 May 2025 16:53:54 +0700 Subject: [PATCH] fix: default route -in --- .../partials/header/locale-switcher.tsx | 18 +++++++++-- i18n/routing.ts | 16 +++++----- middleware.ts | 32 +++++++++++-------- 3 files changed, 42 insertions(+), 24 deletions(-) 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 (