Files
rahavard/middleware.js

48 lines
1.3 KiB
JavaScript
Executable File

import { match } from '@formatjs/intl-localematcher'
import Negotiator from 'negotiator'
// Get the preferred locale, similar to the above or using a library
let locales = ['en', 'fa']
function getLocale(request) {
let headers = { 'accept-language': 'en-US,en;q=0.5' }
let languages = new Negotiator({ headers }).languages()
let defaultLocale = 'en-US';
let domains= [
{
domain: "rahavardmgp.ir",
defaultLocale: 'fa'
},
{
domain: "rahavardmgp.com",
defaultLocale: 'en'
}
]
return match(languages, locales, defaultLocale, domains) // -> 'en-US'
}
export function middleware(request) {
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
)
if (pathnameHasLocale) return
// Redirect if there is no locale
const locale = getLocale(request)
request.nextUrl.pathname = `/${locale}${pathname}`
// e.g. incoming request is /products
// The new URL is now /en-US/products
return Response.redirect(request.nextUrl)
}
export const config = {
matcher: [
// Skip all internal paths (_next)
'/((?!_next).*)',
// Optional: only run on root (/) URL
// '/'
],
}