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
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
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
I do something similar in ex_cldr_routes in the sigil_q macro - that might also give you some ideas.
Last Post!
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 ![]()
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









