added tons of features
This commit is contained in:
25
middlewares/withI18nMiddleware.ts
Normal file
25
middlewares/withI18nMiddleware.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import createMiddleware from 'next-intl/middleware'
|
||||
import {NextFetchEvent, NextRequest, NextResponse} from 'next/server'
|
||||
|
||||
import {routing} from '@/i18n/routing'
|
||||
import {HEADERS} from '@/lib/config/http'
|
||||
import {translatableRoutesRegEx} from '@/lib/config/routes'
|
||||
import {type CustomMiddleware} from '@/middlewares/chain'
|
||||
|
||||
export function withI18nMiddleware(middleware: CustomMiddleware) {
|
||||
return async (
|
||||
request: NextRequest,
|
||||
event: NextFetchEvent,
|
||||
response: NextResponse
|
||||
) => {
|
||||
if (request.nextUrl.pathname.match(translatableRoutesRegEx) !== null) {
|
||||
response = createMiddleware(routing)(request)
|
||||
}
|
||||
|
||||
let locale =
|
||||
request.cookies.get('NEXT_LOCALE')?.value ?? routing.defaultLocale
|
||||
response.headers.set(HEADERS.xSiteLocale, locale)
|
||||
|
||||
return middleware(request, event, response)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user