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

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement