QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.28k stars 59 forks source link

Is it possible to opt out from using cookies or headers? #366

Closed mattiaz9 closed 7 months ago

mattiaz9 commented 7 months ago

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

Right now it gives me the error Locale not found in headers or cookies, returning "notFound()", most likely because I'm not using the default middleware implementation. I don't want to use cookies or headers, since this will cause next to opt out from page caching.

Describe the solution you'd like

using setStaticParamsLocale in the root layout would be great. When I tried it didn't work. Maybe this is an issue?

Describe alternatives you've considered

--

Additional context

I'm using nextjs 14 with the app router and server components.

zfm-lucaschultz commented 7 months ago

Hi, we had the same issue and solved it by using a custom middleware which gets the locale and adds it to the request using a copied and pasted version of addLocaleToResponse. This might lead to other side effects, though.

Overall, I think it would be great to add proper support for this. As I see it, this might not actually need a lot of changes (if any) in the implementation of at least the app router part of this package, but is merely a question of adding documentation and exporting addLocaleToResponse. I may be wrong, though. What do you think, @QuiiBz?

mattiaz9 commented 7 months ago

@zfm-lucaschultz isn't that the same? I mean, if you set the locale in the middleware then you need to access cookies or headers in the page anyway.

What I need is to set the locale directly in the root layout.

Or am I missing something here?

mattiaz9 commented 7 months ago

I ended up creating a custom implementation that uses https://github.com/manvalls/server-only-context on the server to store the current locale and translations

QuiiBz commented 7 months ago

Using setStaticParamsLocale in the root layout would be great. When I tried it didn't work. Maybe this is an issue?

You have to call setStaticParamsLocale() in every page that you want to be static: see https://next-international.vercel.app/docs/app-static-rendering#static-rendering

I'm curious to know why you had to use another middleware implementation?