Current section
Files
Jump to
Current section
Files
lib/ecto_gettext.ex
defmodule EctoGettext do
@moduledoc """
EctoGettext - library for localization Ecto validation errors with using Gettext
### Install:
1. Install Gettext and EctoGettext
2. Import EctoGettext module:
```
import EctoGettext
```
3. Use it
"""
@doc """
Get a list of ecto validation errors and return a new list with translated errors
### Using:
```
localize_validations(MyApp.Gettext, f.errors)
```
"""
@spec localize_validations(module, list) :: list
def localize_validations(module, errors) do
errors
|> Enum.map(fn elem -> localize(module, elem) end)
end
@spec localize(module, tuple) :: {String.t, String.t}
defp localize(module, error) do
{attr, message} = error
{EctoGettext.Attribute.localize_attribute(module, attr),
EctoGettext.Message.localize_message(module, message)}
end
@doc """
Get an attribute name as atom and return a string with translated attribute
### Using:
```
localize_attribute(MyApp.Gettext, :username)
```
"""
@spec localize_attribute(module, atom) :: String.t
def localize_attribute(module, attr) do
EctoGettext.Attribute.localize_attribute(module, attr)
end
@doc """
Get a validation message and return translated message
### Using:
```
localize_message(MyApp.Gettext, {"can't be blank", []})
```
"""
@spec localize_message(module, String.t | tuple) :: String.t
def localize_message(module, message) do
EctoGettext.Message.localize_message(module, message)
end
end