You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Example won't be working because Negotiator will not recognize the NextRequest object that the middleware passes in the next example.
Next example:
letlocales=['en-US','nl-NL','nl']// Get the preferred locale, similar to the above or using a libraryfunctiongetLocale(request){ ... }exportfunctionmiddleware(request){// Check if there is any supported locale in the pathnameconst{ pathname }=request.nextUrlconstpathnameHasLocale=locales.some((locale)=>pathname.startsWith(`/${locale}/`)||pathname===`/${locale}`)if(pathnameHasLocale)return// Redirect if there is no localeconstlocale=getLocale(request)request.nextUrl.pathname=`/${locale}${pathname}`// e.g. incoming request is /products// The new URL is now /en-US/productsreturnResponse.redirect(request.nextUrl)}exportconstconfig={matcher: [// Skip all internal paths (_next)'/((?!_next).*)',// Optional: only run on root (/) URL// '/'],}
Comment from Negotiator's creator: jshttp/negotiator#65 (comment)
Yes, I know the second example says getLocale() is "similar" to above code and not exactly the same. But it would be nice if both examples could work with each other.
Is there any context that might help us understand?
This is the function I wrote using the docs:
import{match}from"@formatjs/intl-localematcher";importNegotiatorfrom"negotiator";exportdefaultfunctiongetLocale(request,locales){const{ headers }=request;constlanguages=newNegotiator({ headers }).languages();// returns ["*"] causing match() to throw an errorconstdefaultLocale="en";returnmatch(languages,locales,defaultLocale);// throws error "Incorrect locale information provided"};
The request param is the request object given by the middleware and the locales is an array of locales.
new Negotiator({ headers }).languages() returns ["*"]. Which causes the match function to throw this error:
⨯ node_modules/@formatjs/intl-localematcher/lib/abstract/CanonicalizeLocaleList.js (7:16) @ getCanonicalLocales
⨯ Incorrect locale information provided
// Negotiator expects plain object so we need to transform headers
const negotiatorHeaders: Record<string, string> = {};
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));
// @ts-ignore locales are readonly
const locales: string[] = i18n.locales;
// Use negotiator and intl-localematcher to get best locale
let languages = new Negotiator({ headers: negotiatorHeaders }).languages(
locales,
);
What is the improvement or update you wish to see?
Example from https://nextjs.org/docs/app/building-your-application/routing/internationalization
Example won't be working because
Negotiator
will not recognize theNextRequest
object that the middleware passes in the next example.Next example:
Comment from Negotiator's creator: jshttp/negotiator#65 (comment)
Yes, I know the second example says
getLocale()
is "similar" to above code and not exactly the same. But it would be nice if both examples could work with each other.Is there any context that might help us understand?
This is the function I wrote using the docs:
The
request
param is the request object given by the middleware and thelocales
is an array of locales.new Negotiator({ headers }).languages()
returns["*"]
. Which causes thematch
function to throw this error:The middleware:
Does the docs page already exist? Please link to it.
https://nextjs.org/docs/pages/building-your-application/routing/internationalization
The text was updated successfully, but these errors were encountered: