From c4a35e97c34891f971a864914dbfc21bc38a7e19 Mon Sep 17 00:00:00 2001 From: nxtkofi Date: Tue, 21 Apr 2026 20:53:09 +0200 Subject: [PATCH] feat(i18n): add locale-based routing setup Add next-intl routing config, proxy middleware, and update request config to use requestLocale from middleware. Add @/i18n/* path alias to tsconfig. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- src/i18n/request.ts | 18 ++++++++---------- src/i18n/routing.ts | 7 +++++++ src/proxy.ts | 10 ++++++++++ tsconfig.json | 1 + 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 src/i18n/routing.ts create mode 100644 src/proxy.ts diff --git a/src/i18n/request.ts b/src/i18n/request.ts index 0fff0f9..380268d 100644 --- a/src/i18n/request.ts +++ b/src/i18n/request.ts @@ -1,14 +1,12 @@ -import { getRequestConfig } from "next-intl/server"; -import { headers } from "next/headers"; +import { getRequestConfig } from 'next-intl/server'; +import { hasLocale } from 'next-intl'; +import { routing } from './routing'; -export default getRequestConfig(async () => { - const headersList = await headers(); - const acceptLanguage = headersList.get("accept-language"); - - const browserLocale = acceptLanguage?.split(",")[0]?.split("-")[0]; - - const supportedLocales = ["en", "pl"]; - const locale = browserLocale && supportedLocales.includes(browserLocale) ? browserLocale : "en"; +export default getRequestConfig(async ({ requestLocale }) => { + const requested = await requestLocale; + const locale = hasLocale(routing.locales, requested) + ? requested + : routing.defaultLocale; return { locale, diff --git a/src/i18n/routing.ts b/src/i18n/routing.ts new file mode 100644 index 0000000..113b473 --- /dev/null +++ b/src/i18n/routing.ts @@ -0,0 +1,7 @@ +import { defineRouting } from 'next-intl/routing'; + +export const routing = defineRouting({ + locales: ['en', 'pl'], + defaultLocale: 'en', + localePrefix: 'as-needed', +}); diff --git a/src/proxy.ts b/src/proxy.ts new file mode 100644 index 0000000..b58c790 --- /dev/null +++ b/src/proxy.ts @@ -0,0 +1,10 @@ +import createMiddleware from 'next-intl/middleware'; +import { routing } from './i18n/routing'; + +export default createMiddleware(routing); + +export const config = { + matcher: [ + '/((?!api|_next|_vercel|.*\\..*).*)', + ], +}; diff --git a/tsconfig.json b/tsconfig.json index ff5fc1c..336baaa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,6 +24,7 @@ "@/lib/*": ["./src/lib/*"], "@/hooks/*": ["./src/hooks/*"], "@/app/*": ["./src/app/*"], + "@/i18n/*": ["./src/i18n/*"], "@/constants": ["./src/constants.ts"] } },