Skip to content

Instantly share code, notes, and snippets.

@FabienHenon
Created June 2, 2017 10:53
Show Gist options
  • Save FabienHenon/b17f9c2724adcc80526f11a88ab80476 to your computer and use it in GitHub Desktop.
Save FabienHenon/b17f9c2724adcc80526f11a88ab80476 to your computer and use it in GitHub Desktop.
defmodule ModContact.Types.FieldTypes do
import Utils.Gettext
@external_resource field_types_path = Path.join([__DIR__, "res", "field_types.json"])
# Loads field types in module attributes
Module.register_attribute __MODULE__, :field_types, accumulate: true
{:ok, json} =
with {:ok, body} <- File.read(field_types_path),
{:ok, json} <- Poison.decode(body), do: {:ok, json}
Enum.filter(json, &Map.has_key?(&1, "id"))
|> Enum.map(&Module.put_attribute(__MODULE__, :field_types, &1))
# For each exising locale we define the field_types function that will translate name
for locale <- Application.get_env(:utils, Utils.Gettext)[:locales] do
field_types =
Enum.map(@field_types, fn field_type ->
if Map.has_key?(field_type, "name") do
IO.inspect(unquote(field_type["name"]))
Map.put(field_type, "name", dgettext("field_types", unquote(field_type["name"])))
else
field_type
end
end)
def field_types(unquote(locale)), do: unquote(Macro.escape(field_types))
end
@doc """
Returns field types list
"""
def field_types(_locale), do: field_types(Application.get_env(:utils, :default_locale, "fr"))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment