It depends on your requirements (You can go with local storage or cookies or even combine them), but to help you breaking it up:
- Local storage persists even after the browser is closed and reopened, making it good for storing user preferences that should be maintained across sessions, but it won't be available during the SSR.
- Session storage is only available for the duration of the page session, and it won't be available during the SSR. So it is not really an option for the language, I suppose.
- With Cookies 🍪 it is useful for server-side operations.
Sooo, Local storage is only available on the client side. So if you want to use it, you can create a hook with a
if (typeof window !== 'undefined'){
// Accessing Local storage
}
You can check this: https://hackernoon.com/storing-and-retrieving-data-in-nextjs-using-localstorage-and-typescript
For cookies, you can also use them on the client side with next-client-cookies
Also, if you use a localization library like i18nexus it saves the locale in the cookies too (NEXT_LOCALE) :) You can check this tutorial for it https://i18nexus.com/tutorials/nextjs/react-i18next
For me, I would use cookies.
Hope this answer helps!