next-intl auto-detection w/ middleware + localePrefix: 'as-needed' always returns 'en' on first visit, am I misconfiguring? #1986
-
Beta Was this translation helpful? Give feedback.
Answered by
Adebesin-Cell
Aug 15, 2025
Replies: 1 comment 4 replies
-
Solved! 🎉 // routing.ts
import { localeSchema } from "@/messages/_schema";
import { createNavigation } from "next-intl/navigation";
import { defineRouting } from "next-intl/routing";
export const routing = defineRouting({
locales: localeSchema.options,
defaultLocale: "en",
localePrefix: {
mode: "as-needed",
prefixes: {
ko: "/kr",
},
},
localeDetection: true,
});
export const { Link, redirect, usePathname, useRouter } =
createNavigation(routing); // request.ts
import { getRequestConfig } from "next-intl/server";
import { routing } from "./routing";
import { localeSchema } from "@/messages/_schema";
export default getRequestConfig(async ({ requestLocale }) => {
let locale = await requestLocale;
if (!locale || !routing.locales.includes(locale)) {
locale = routing.defaultLocale;
}
if (locale === localeSchema.Enum.ko) {
locale = localeSchema.Enum.kr;
}
return {
locale,
messages: (await import(`../messages/${locale}.json`)).default,
};
}); |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
Adebesin-Cell
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved! 🎉
I ended up fixing it by mapping
ko
→kr
directly in mygetRequestConfig
and adding a prefix mapping indefineRouting
.