I am using next-international package for i18n in my nextjs application.
Mostly working well but I want to customize the prefix of the locale.
In short, what we want is localhost:3000/
for default locale, but localhost:3000/us
for english locale, not localhost:3000/en
from accept-languages. But, want to keep using browser language detection.
In my middleware.ts, currently only works with /en
.
export const I18nMiddleware = createI18nMiddleware({
locales: ["en", "de"],
defaultLocale: "de",
urlMappingStrategy: "rewriteDefault",
})
export default async function middleware(request: NextRequest) {
return I18nMiddleware(request)
}
Because we detect the first language from browser header (accept-language), we can’t set like locales: ['us', 'de']
since browser just tell us
en value otherwise it redirects to the defaultLocale.
Is there anyway to handle this?
2
Answers
I didn’t try, but I think you can try:
so you can update:
You can do this using
resolveLocaleFromRequest
method from next-international: