coen.bakker

coen.bakker

Why is Gettext not getting a string at compile-time in the macro I built?

I was writing a macro for injecting internationalised routes into MyAppWeb.Router.

When using the Gettext macros, in contrast to the Gettext functions, the msgid argument must be a string at compile-time (see docs).

I am confused about why that’s not the case here:

defmodule MyAppWeb.LocaleRoutes do
  require MyAppWeb.Gettext
  import MyAppWeb.Gettext

  @display_languages ["en", "nl", "pt", "fil", "zh"]

  defmacro __using__(_) do
    quote do
      require unquote(__MODULE__)
      import unquote(__MODULE__)
    end
  end

  defmacro live_int(path, live_view) do
    for lang <- @display_languages do
      IO.inspect(path) # For example: "welcome"
      path_int = "/#{lang}/#{Gettext.with_locale(lang, fn -> dgettext("routes", path) end)}"

      quote do
        live unquote(path_int), unquote(live_view)
      end
    end
  end

  defmacro get_int(path, plug, plug_opts) do
    for lang <- @display_languages do
      path_int = "/#{lang}/#{Gettext.with_locale(lang, fn -> dgettext("routes", path) end)}"

      quote do
        live unquote(path_int), unquote(plug), unquote(plug_opts)
      end
    end
  end
end

Gettext complains that it’s not getting a string at compile-time, but instead is getting a AST node: {:path, [line: 16, column: 81], nil}. But since the path is not user generated, but defined in MyAppWeb.Router, I expected the msgid to be a string at compile-time.

Most Liked

kip

kip

ex_cldr Core Team

Mostly, but not always. Gettext will also accept:

iex> quote do
...> "This is" <> "a string"
...> end
{:<>, [context: Elixir, imports: [{2, Kernel}]], ["This is", "a string"]}
kip

kip

ex_cldr Core Team

Something like this is the approach I would probably take:

defmodule MyApp.Macro do
  @display_languages ["en", "nl", "pt", "fil", "zh"]

  defmacro live_int(path, live_view) do
    for lang <- @display_languages do
      quote do
        translation =
          Gettext.with_locale(unquote(lang), fn ->
            MyApp.Gettext.dgettext("routes", unquote(path))
          end)

        path_int =
          "/#{unquote(lang)}/#{translation}"

        live unquote(path_int), unquote(live_view)
      end
    end
  end
end
kip

kip

ex_cldr Core Team

I do something similar in ex_cldr_routes in the sigil_q macro - that might also give you some ideas.

Last Post!

sodapopcan

sodapopcan

Ya I don’t know what I was thinking yesterday other than my brain getting super tripped up by a problem I ran into in another language :woozy_face:

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement