Optional default locale in pathname with App Router
Unanswered
Barbary Lion posted this in #help-forum
Barbary LionOP
We're building a new Next.js application using the App Directory. Is it possible to make the default locale optional in the pathname when localizing like you can with Pages? For example,
domain.com resolves the same content as domain.com/en. We're using the app directory structure of app/[lang] and following the docs (https://nextjs.org/docs/app/building-your-application/routing/internationalization). We've also tried making the lang parameter optional via [[lang]] but we're getting the following error: Optional route parameters are not yet supported ("[[lang]]"). I'm hoping there is another way around this in the meantime 🙂2 Replies
@Barbary Lion We're building a new Next.js application using the App Directory. Is it possible to make the default locale optional in the pathname when localizing like you can with Pages? For example, `domain.com` resolves the same content as `domain.com/en`. We're using the app directory structure of `app/[lang]` and following the docs (https://nextjs.org/docs/app/building-your-application/routing/internationalization). We've also tried making the `lang` parameter optional via `[[lang]]` but we're getting the following error: `Optional route parameters are not yet supported ("[[lang]]")`. I'm hoping there is another way around this in the meantime 🙂
you can use middleware to rewrite from requests without the lang value to requests with the lang value. pseudocode:
middleware(request):
if request starts with /en: continue
if request starts with /fr: continue
go to /en/request.pathname
matcher: match only pages, not static filesbut tbh the app router doesn't support optional default locale all that well, just use a locale pathname as they want you to do, it doesn't look that bad either