I have this module
defmodule ElixirMeta.LangLoader do
@external_resource [Path.join([__DIR__, "es.json"]),
Path.join([__DIR__, "en.json"])]
defmacro __using__(_) do
for lang <- ["es", "en"] do
{:ok, body} = File.read(Path.join([__DIR__, "#{lang}.json"]))
{:ok, json} = Poison.decode(body)
quote do
def lang(unquote(lang)), do: unquote(Macro.escape(json))
end
end
end
end
defmodule ElixirMeta.Lang do
use ElixirMeta.LangLoader
end
I know I can define a function like:
def lang(unquote(lang)), do: unquote(Macro.escape(json))
And can be called like this:
Lang.lang("es")
Also even modify it's function name, like this:
def unquote(:"lang_#{lang}")(), do: unquote(Macro.escape(json))
And be called like this:
Lang.lang_es
But is it possible to do the same with a module attribute?
And being the module attribute compiled (?) I think is not possible to initialize it from the macro? maybe I would have to do it within before_compile
macro?
I would like to access, for the purpose of the example, Lang.lang_es
as a @lang_es
and @lang_en
LangLoader
attributes