-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
language function does not return the expected language in nextjs middleware #65
Comments
Unfortunately it doesn't look like the |
Hey @kaganAhmetOkan. The Next.js team kinda shows what could be done in these docs, but not really: https://nextjs.org/docs/app/building-your-application/routing/internationalization Like @dougwilson says, the Next.js request object isn't compatible, but you can just pass the headers to Negotiator. So you could do something like: const locales = ['en', 'tr'];
let locale = 'en' // your default locale
const languageHeaders = request.headers.get('Accept-Language')
if (languageHeaders) {
locale = new Negotiator({
headers: {'accept-language': languageHeaders},
}).language(locales)
} |
Hey @jmagrippis, thank you for your help. I had not thought about passing the accept-language header directly. const locales = ["en", "tr"];
const defaultLocale = "en";
const languageHeaders = request.headers.get("Accept-Language");
const locale = new Negotiator({ headers: {"accept-language": languageHeaders }}).language(locales);
return locale ?? defaultLocale; |
in
middleware.js
:curl -v --header "Accept-Language: tr" localhost:3000
redirects me to/en
regardless of the accept-language header.The whole middleware:
I don't really understand what I'm getting wrong here. Excuse me if I'm missing something very simple.
The text was updated successfully, but these errors were encountered: