romaluca

romaluca

Gettext and liveview

Hi,
I have a web app with default locale: it
I created a liveviewmodule like this:


defmodule ShuttertopWeb.UserFollowLive do
    use ShuttertopWeb, :live_view

def render(assigns) do
      Logger.info("------ #{inspect(Gettext.get_locale(ShuttertopWeb.Gettext))}")
      ~L"""
      <button phx-click="follow" class="<%= if @active, do: "actived" %>">
          <%= if(@active, do: gettext("seguendo"), else: gettext("segui")) %>
      </button>
      """
    end

    def mount(_params, %{"user_id" => user_id, "current_user_id" => current_user_id, "active" => active}, socket) do

      socket =
        socket
        |> assign(%{
          active: active,
          current_user_id: current_user_id,
          user_id: user_id,
        })
      {:ok, socket, layout: false}
    end

....
end

If i launch my site using an user with locale: en, the page html is render correctly in english but in few milliseconds the button text changes and become italian text.

This is the log that i put in render method:

info] ------ "en"
[info] Sent 200 in 22ms
[info] CONNECTED TO Phoenix.LiveView.Socket in 249µs
  Transport: :websocket
  Serializer: Phoenix.Socket.V2.JSONSerializer
  Parameters: %{"_csrf_token" => "QTUALCk4NAhdFQgCWVgtLhFrD2EaJQZj-jpOAALj6dDjoahiA4W1Bjl3", "_mounts" => "0", "vsn" => "2.0.0"}
[info] ------ "it"

How can i do to maintain the locale en?

Thanks

Marked As Solved

josevalim

josevalim

Creator of Elixir

There is also a quick guide on the docs showing how to do that: https://hexdocs.pm/phoenix_live_view/using-gettext.html#content

Also Liked

LostKobrakai

LostKobrakai

Just like you likely do in your plug pipeline you’ll need to set the locale for your liveview process as well. Because the first static render of your liveview does happen in the same process as your plug pipeline the locale is retained for the static render. That’s not the case though for any render happening later when the liveview js connects.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement