middleware.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { match } from '@formatjs/intl-localematcher'
  2. import Negotiator from 'negotiator'
  3. import { NextResponse } from 'next/server'
  4. import type { NextRequest } from 'next/server'
  5. import type { Locale } from './i18n'
  6. import { i18n } from './i18n'
  7. export const getLocale = (request: NextRequest): Locale => {
  8. // @ts-expect-error locales are readonly
  9. const locales: Locale[] = i18n.locales
  10. let languages: string[] | undefined
  11. // get locale from cookie
  12. const localeCookie = request.cookies.get('locale')
  13. languages = localeCookie?.value ? [localeCookie.value] : []
  14. if (!languages.length) {
  15. // Negotiator expects plain object so we need to transform headers
  16. const negotiatorHeaders: Record<string, string> = {}
  17. request.headers.forEach((value, key) => (negotiatorHeaders[key] = value))
  18. // Use negotiator and intl-localematcher to get best locale
  19. languages = new Negotiator({ headers: negotiatorHeaders }).languages()
  20. }
  21. // match locale
  22. let matchedLocale:Locale = i18n.defaultLocale
  23. try {
  24. // If languages is ['*'], Error would happen in match function.
  25. matchedLocale = match(languages, locales, i18n.defaultLocale) as Locale
  26. } catch(e) {}
  27. return matchedLocale
  28. }
  29. export const middleware = async (request: NextRequest) => {
  30. const pathname = request.nextUrl.pathname
  31. if (/\.(css|js(on)?|ico|svg|png)$/.test(pathname))
  32. return
  33. const locale = getLocale(request)
  34. const response = NextResponse.next()
  35. response.cookies.set('locale', locale)
  36. return response
  37. }