Open kaganAhmetOkan opened 9 months ago
Same issue in v14.0.4 (with default locale defined as 'fr')
probably fixed with this answer :
This should do the trick
// 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,
);
Taken from: https://github.com/vercel/next.js/blob/canary/examples/app-dir-i18n-routing/middleware.ts
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: https://github.com/jshttp/negotiator/issues/65#issuecomment-1815325586 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