Gettext usage in LiveView templates (leex) not picked up via mix.gettext

When using something like:

<%= Gettext.gettext(JustifiedWeb.Gettext, "Entiteit") %>

in LiveView templates (.leex) and when running

mix gettext.extract
mix gettext.merge priv/gettext

to extract and merge all gettext references to our .pot and .po files.
These references never get picked up. I could add the msgid and msgstr manually, but those are overwritten when extracting and merging again.

Is there some configuration setting that I’m missing? A place I forgot to import Gettext?

Does it make a difference if you simply use gettext/2 which should have been imported from YourAppWeb.Gettext into all views?

It is not available in the short form in my .leex files, although I have it imported into all views undefined function gettext/2.

How about YourAppWeb.Gettext.gettext/2?

I think I found the solution while testing the reply of @wmnnd
Apparently, you need to import import YourAppWeb.Gettext into the corresponding live module of the leex template.

Works:

defmodule YourAppWeb.SomethingLive do
  use Phoenix.LiveView
  use Phoenix.HTML

  import YourAppWeb.Gettext

  def mount(_, _, socket) do
    {:ok, socket}
  end
end

<%= gettext("Entiteit") %>

Does not work:

defmodule YourAppWeb.SomethingLive do
  use Phoenix.LiveView
  use Phoenix.HTML

  # import YourAppWeb.Gettext

  def mount(_, _, socket) do
    {:ok, socket}
  end
end

<%= gettext("Entiteit") %>

Now, it seems obvious. :slight_smile:

3 Likes

Hello

I put it in the app_web.ex file in the view_helpers function so it is available from everywhere.

cheers,
Sébastien.

1 Like

We had that too, but it didn’t seem to work.

Stumbled across your message a bit late, but maybe my findings are still helpful, or helpful to others – I had the same error when mistakenly doing something like this:

defmodule FooWeb.SomeComponent
  use Phoenix.LiveComponent
# ...
end

Instead, you are supposed to use the macros generated for you:

defmodule FooWeb.SomeComponent
  use FooWeb, :live_component
# ...
end

This way, all view helpers are imported – basically that is everything the use FooWeb, :live_component macro does (at least in code generated using liveview version 0.15.5) in addition to use Phoenix.LiveComponent, so it takes some time until you find out that you have some functionality missing.