Hello and welcome,
It is a little bit more complicate to pass js data to the server, because http is not bidirectional.
Are You sure You cannot do all client side? recently I had to display datetime in the browser timezone. It was simpler to add some class to datetime container (“utc_to_local”) and make the change client side only.
But if You want to make it server side, I think the easiest solution would be to pass the locale, like new?locale=whatever. Because new is a get request.
You could use a plug to set a session_id.
This way, You could pass data to the server with this session_id, and retrieve this data server side
It’s not even needed, and it is nice to see how it is done in liveview, which use websocket under the hood. You can set params to the socket, and retrieve them in liveview.
let liveSocket = new LiveSocket("/live", Socket, {
params: {
_csrf_token: csrfToken,
locale: Intl.NumberFormat().resolvedOptions().locale,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
timezone_offset: -(new Date().getTimezoneOffset() / 60),
}
});
# In liveview mount
if connected?(socket) do
Logger.info("SOCKET PARAMS: #{inspect get_connect_params(socket)}")
end
# And the result
[info] SOCKET PARAMS: %{
"_csrf_token" => "...",
"_mounts" => 0,
"_track_static" => ["..."],
"locale" => "fr",
"timezone" => "Europe/Zurich",
"timezone_offset" => 2
}
So You could use websocket, but it does not seems You need this, You just want to localize your login form. => new?locale=whatever and retrieve the locale server side.
Use an id to the new link, and add locale params to the path of this link with js.