2023-05-15 08:51:32 +08:00
|
|
|
import { match } from '@formatjs/intl-localematcher'
|
|
|
|
import Negotiator from 'negotiator'
|
|
|
|
import { NextResponse } from 'next/server'
|
|
|
|
import type { NextRequest } from 'next/server'
|
|
|
|
import type { Locale } from './i18n'
|
|
|
|
import { i18n } from './i18n'
|
|
|
|
|
|
|
|
export const getLocale = (request: NextRequest): Locale => {
|
|
|
|
// @ts-expect-error locales are readonly
|
|
|
|
const locales: Locale[] = i18n.locales
|
|
|
|
|
|
|
|
let languages: string[] | undefined
|
|
|
|
// get locale from cookie
|
|
|
|
const localeCookie = request.cookies.get('locale')
|
|
|
|
languages = localeCookie?.value ? [localeCookie.value] : []
|
|
|
|
|
|
|
|
if (!languages.length) {
|
|
|
|
// Negotiator expects plain object so we need to transform headers
|
|
|
|
const negotiatorHeaders: Record<string, string> = {}
|
|
|
|
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value))
|
|
|
|
// Use negotiator and intl-localematcher to get best locale
|
|
|
|
languages = new Negotiator({ headers: negotiatorHeaders }).languages()
|
|
|
|
}
|
|
|
|
|
|
|
|
// match locale
|
2023-05-30 10:32:26 +08:00
|
|
|
let matchedLocale: Locale = i18n.defaultLocale
|
2023-05-20 17:12:12 +08:00
|
|
|
try {
|
|
|
|
// If languages is ['*'], Error would happen in match function.
|
|
|
|
matchedLocale = match(languages, locales, i18n.defaultLocale) as Locale
|
2023-05-30 10:32:26 +08:00
|
|
|
}
|
|
|
|
catch (e) {}
|
2023-05-15 08:51:32 +08:00
|
|
|
return matchedLocale
|
|
|
|
}
|
|
|
|
|
|
|
|
export const middleware = async (request: NextRequest) => {
|
|
|
|
const pathname = request.nextUrl.pathname
|
|
|
|
if (/\.(css|js(on)?|ico|svg|png)$/.test(pathname))
|
|
|
|
return
|
|
|
|
|
|
|
|
const locale = getLocale(request)
|
|
|
|
const response = NextResponse.next()
|
|
|
|
response.cookies.set('locale', locale)
|
|
|
|
return response
|
|
|
|
}
|