How to access browser language during SSR?
In an
SPA
you would normally access navigator.language
but during SSR
the navigator
is undefined. How can I access the browser language/locale during SSR?13 Replies
I have a helper that im using in my projects.
First i check if the user has the langugae cookie settet, indicating that the user has changed the language. Second take the first langage in the accept-language.
Last i splitt the lang, for example if it is en-US i only need the en and check it against available languages that my app supports. If it is a language that i dont support i fallback to the apps default locale
Where does the
IncomingMessage
come from?
Ah... got it (I think)I was just looking in the wrong direction: https://start.solidjs.com/api/useServerEvent#accessing-the-request-on-the-server
SolidStart Beta Docuentation
SolidStart Beta Documentation
Early release documentation and resources for SolidStart Beta
Using it in multiple nextjs applications, havent tried in in solid but should work without problems. Using it like this in my application:
In solid it's different. Though I'm still having troubles. When using
useServerContext()
I just get an empty object.Hmm okej, will try on my solid-project!
It's probably because I'm trying to use
useServerContext
outside a component.
Jepp. That's the reason. The server context is only available as soon as the root component is rendered. Just got it working.Yes got it working to! Mine looks like this:
And if i change browser language i get different languages in the div
That's how my code looks. I'll implement language switching at a higher level. This is just my helper method for now:
Do you get the correct langage on the html-tag?
Yes.
Nice, did you run browserLanguage in root.tsx?
Initially I just had it as part of an imported module. But that caused
useServerContext
to return an empty object. After putting it into the root render function it did return the proper values.