fix: default route -in

This commit is contained in:
Rama Priyanto 2025-05-22 16:53:54 +07:00
parent 988dc1e3c6
commit 4382b55196
3 changed files with 42 additions and 24 deletions

View File

@ -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 (
<Select onValueChange={onSelectChange} defaultValue={localActive}>

View File

@ -1,15 +1,15 @@
import {defineRouting} from 'next-intl/routing';
import {createSharedPathnamesNavigation} from 'next-intl/navigation';
import {locales} from '@/config';
import { defineRouting } from "next-intl/routing";
import { createSharedPathnamesNavigation } from "next-intl/navigation";
import { locales } from "@/config";
export const routing = defineRouting({
// A list of all locales that are supported
locales: locales,
// Used when no locale matches
defaultLocale: 'en'
defaultLocale: "in",
});
// Lightweight wrappers around Next.js' navigation APIs
// that will consider the routing configuration
export const {Link, redirect, usePathname, useRouter} =
createSharedPathnamesNavigation(routing);
export const { Link, redirect, usePathname, useRouter } =
createSharedPathnamesNavigation(routing);

View File

@ -1,25 +1,29 @@
import createMiddleware from "next-intl/middleware";
import { NextRequest, NextResponse } from "next/server";
import { locales } from "@/config";
import { routing } from "./i18n/routing";
export default async function middleware(request: NextRequest) {
// Step 1: Use the incoming request (example)
const defaultLocale = request.headers.get("dashcode-locale") || "in";
// export default async function middleware(request: NextRequest) {
// // Step 1: Use the incoming request (example)
// const defaultLocale = "in";
// // const defaultLocale = request.headers.get("dashcode-locale") || "in";
// Step 2: Create and call the next-intl middleware (example)
const handleI18nRouting = createMiddleware({
locales,
defaultLocale,
});
const response = handleI18nRouting(request);
// // Step 2: Create and call the next-intl middleware (example)
// const handleI18nRouting = createMiddleware({
// locales: ["in", "en"],
// defaultLocale: "in",
// });
// const response = handleI18nRouting(request);
// Step 3: Alter the response (example)
response.headers.set("dashcode-locale", defaultLocale);
// // Step 3: Alter the response (example)
// response.headers.set("dashcode-locale", defaultLocale);
return response;
}
// return response;
// }
export default createMiddleware(routing);
export const config = {
// Match only internationalized pathnames
matcher: ["/", "/(ar|in|en)/:path*"],
matcher: ["/", "/(in|en)/:path*"],
};