longnightElixir
I am building a language switcher (“en”, “de”, “fr”, etc. select input box) of my website;
when user first access, I detemine their accept-language from request headers, and set it in current user’s session; the user may be guest/annonymous so they have no backend data records;
Then currrent user may switch to other prefer language; (!! inside liveview I have no ability to persist this changed state into session and cookie! and I think update to url query params is not elegant so I donot consider of that way)
Depends on the language we render corresponses content to user;
But when user navigate to other liveview, or refresh page, or “duplicate tab page” in their browser, we still offer the default detected language/content but not user prefer language/content, that’s not user expected.
so , do you have any advices? Thanks !
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sodapopcan
If you want to use cookies, just use a controller action to set it and redirect back to the liveview. Sure it’s a page reload but it’s not like they’ll be switching constantly.
Another option is to store it in the database.
kokolegorille
Sometime I share a link… and I’d like to be able to set the language in it…
You can also use LocalStorage if You don’t need to ask the backend
longnightElixir
It seems that preparing db records for guest user is the last option I have
longnightElixir
place it in url means that in all future navigate/patch I have to maintain this state …
greven
For guests you could use the browser local storage.
longnightElixir
Thanks for replied. But browser local storage do not come with request
greven
What do you mean it doesn’t come with request?
You can read/write to the storage with a simple hook.
If it is a guest read/write from Local Storage, if the user upgrades to a DB backed account, you can read the local storage and store it in the DB, henceforth just give priority to the DB.
You can choose the sessionStorage or the localStorage if you want to persist after the tab closes.
EDIT - Here’s a tutorial on a Hook for sessionStorage in this case: Saving and Restoring LiveView State · The Phoenix Files
longnightElixir
I had never know that… after read that article, yes, it is a better approach ! thank you for share it ! Just a litter cons, it relied on client side javascript, … I have to reconsider my actual needs.
greven
There is no problem relying on the client for things like this. There is no data privacy involved nor much complexity added. Embrace JS when you have to.
sodapopcan
I agree with this sentiment 100%, but I gotta ask: why is this better in this situation than using a controller? (…other than a page refresh, of course.) If the translation is done on the backend, a JS hook means there will be a jump between default and selected languages when the websocket upgrades. At least this is the problem I’ve run into when using localStorage for stuff like this. You can skip the dead render but since OP’s situation involves guest users that don’t have db entries, I assume these are public pages so you’d want to keep it.
The controller version is also much simpler. The whole thing is:
Am I missing something?
Also, I do agree that it’s generally better to put it in the URL, especially if URLs are meant to be shared (if it’s a closed app it’s not as big of a deal). There are libraries to help with this, like cldr_routes and the newer routex.