amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.58k stars 236 forks source link

Just like `getLocale`, why isn't there a `setLocale`? #1381

Closed forhadakhan closed 1 month ago

forhadakhan commented 1 month ago

Is your feature request related to a problem? Please describe.

I tried using App Router setup without i18n routing and defined a local switcher following official example.

This solution worked fine in the local development environment but resulted in an error (Cookies can only be modified in a Server Action or Route Handler. nextjs 14.2.13) in the production environment on Vercel."

Describe the solution you'd like

I think next-intl should have a direct utility to set the locale from both client and server components. Just as we can access the current locale using getLocale in server or useLocale in client, there should be a similar feature for setting the locale.

NOTE: If I'm missing something or if such solutions already exist, please point me to the direct solution.

Describe alternatives you've considered

Alternative solutions might vary from developer to developer. I am working on a way to address this, but it would be better if there were an official solution.

amannn commented 1 month ago

The implementation of setLocale really depends on how you want to manage the locale in your app. It can be a cookie, a setting in the user profile, etc.

If it's a cookie, you can use the approach shown in example-app-router-without-i18n-routing—this example works fine on Vercel.

I'll move this to a discussion since it's a usage question.