longnightElixir

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 !

Showing Posts 1 to 10

sodapopcan

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

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

longnightElixir OP

It seems that preparing db records for guest user is the last option I have :joy:

longnightElixir

longnightElixir OP

place it in url means that in all future navigate/patch I have to maintain this state … :slightly_smiling_face:

greven

greven

For guests you could use the browser local storage.

longnightElixir

longnightElixir OP

Thanks for replied. But browser local storage do not come with request :slightly_smiling_face:

greven

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

longnightElixir OP

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

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. :wink:

sodapopcan

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:

defmodule LangController do
  use Phoenix.Controller

  def update(conn, params) do
    conn
    |> put_session(:language, params["lang"])
    |> redirect(to: params["return_to"])
  end
end

# router
post "/set-language", LangController, :update

# heex
<form action={~p"/set-language"} method="post">
  <input type="hidden" name="return_to" value={~p"/current-url"} />
  <select name="lang">
    <option>...</option>
  </select
</form>

# lv
def mount(_params, %{"lang" => lang}, socket) do
  # ...
end

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.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews