fix: default route -in
This commit is contained in:
parent
988dc1e3c6
commit
4382b55196
|
|
@ -5,7 +5,7 @@ import { useParams } from "next/navigation";
|
||||||
import { locales } from "@/config";
|
import { locales } from "@/config";
|
||||||
import { usePathname, useRouter } from "@/i18n/routing";
|
import { usePathname, useRouter } from "@/i18n/routing";
|
||||||
|
|
||||||
import { useTransition } from "react";
|
import { useEffect, useTransition } from "react";
|
||||||
import {
|
import {
|
||||||
Select,
|
Select,
|
||||||
SelectContent,
|
SelectContent,
|
||||||
|
|
@ -14,18 +14,32 @@ import {
|
||||||
SelectValue,
|
SelectValue,
|
||||||
} from "@/components/ui/select";
|
} from "@/components/ui/select";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
export default function LocalSwitcher() {
|
export default function LocalSwitcher() {
|
||||||
const [isPending, startTransition] = useTransition();
|
const [isPending, startTransition] = useTransition();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const params = useParams();
|
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) => {
|
const onSelectChange = (nextLocale: string) => {
|
||||||
startTransition(() => {
|
startTransition(() => {
|
||||||
router.replace(pathname, { locale: nextLocale });
|
router.replace(pathname, { locale: nextLocale });
|
||||||
});
|
});
|
||||||
|
Cookies.set("locale", nextLocale);
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<Select onValueChange={onSelectChange} defaultValue={localActive}>
|
<Select onValueChange={onSelectChange} defaultValue={localActive}>
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
import {defineRouting} from 'next-intl/routing';
|
import { defineRouting } from "next-intl/routing";
|
||||||
import {createSharedPathnamesNavigation} from 'next-intl/navigation';
|
import { createSharedPathnamesNavigation } from "next-intl/navigation";
|
||||||
import {locales} from '@/config';
|
import { locales } from "@/config";
|
||||||
export const routing = defineRouting({
|
export const routing = defineRouting({
|
||||||
// A list of all locales that are supported
|
// A list of all locales that are supported
|
||||||
locales: locales,
|
locales: locales,
|
||||||
|
|
||||||
// Used when no locale matches
|
// Used when no locale matches
|
||||||
defaultLocale: 'en'
|
defaultLocale: "in",
|
||||||
});
|
});
|
||||||
|
|
||||||
// Lightweight wrappers around Next.js' navigation APIs
|
// Lightweight wrappers around Next.js' navigation APIs
|
||||||
// that will consider the routing configuration
|
// that will consider the routing configuration
|
||||||
export const {Link, redirect, usePathname, useRouter} =
|
export const { Link, redirect, usePathname, useRouter } =
|
||||||
createSharedPathnamesNavigation(routing);
|
createSharedPathnamesNavigation(routing);
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,29 @@
|
||||||
import createMiddleware from "next-intl/middleware";
|
import createMiddleware from "next-intl/middleware";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { locales } from "@/config";
|
import { locales } from "@/config";
|
||||||
|
import { routing } from "./i18n/routing";
|
||||||
|
|
||||||
export default async function middleware(request: NextRequest) {
|
// export default async function middleware(request: NextRequest) {
|
||||||
// Step 1: Use the incoming request (example)
|
// // Step 1: Use the incoming request (example)
|
||||||
const defaultLocale = request.headers.get("dashcode-locale") || "in";
|
// const defaultLocale = "in";
|
||||||
|
// // const defaultLocale = request.headers.get("dashcode-locale") || "in";
|
||||||
|
|
||||||
// Step 2: Create and call the next-intl middleware (example)
|
// // Step 2: Create and call the next-intl middleware (example)
|
||||||
const handleI18nRouting = createMiddleware({
|
// const handleI18nRouting = createMiddleware({
|
||||||
locales,
|
// locales: ["in", "en"],
|
||||||
defaultLocale,
|
// defaultLocale: "in",
|
||||||
});
|
// });
|
||||||
const response = handleI18nRouting(request);
|
// const response = handleI18nRouting(request);
|
||||||
|
|
||||||
// Step 3: Alter the response (example)
|
// // Step 3: Alter the response (example)
|
||||||
response.headers.set("dashcode-locale", defaultLocale);
|
// response.headers.set("dashcode-locale", defaultLocale);
|
||||||
|
|
||||||
return response;
|
// return response;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
export default createMiddleware(routing);
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
// Match only internationalized pathnames
|
// Match only internationalized pathnames
|
||||||
matcher: ["/", "/(ar|in|en)/:path*"],
|
matcher: ["/", "/(in|en)/:path*"],
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue