# EctoGettext
[![Build Status](https://travis-ci.org/exbugs-elixir/ecto_gettext.svg?branch=master)](https://travis-ci.org/exbugs-elixir/ecto_gettext)
EctoGettext - library for localization Ecto validation errors with using Gettext
## Installation
The package can be installed as:
Add ecto_gettext to your list of dependencies in `mix.exs` and run `mix deps.get`:
```elixir
def deps do
[{:ecto_gettext, "~> 0.1.6"}]
end
```
## Usage
1. Create Gettext module (if it is not):
```elixir
defmodule MyApp.Gettext do
use Gettext, otp_app: :my_app
end
```
2. Add this line into "view" section in the web/web.ex (if you use phoenix framework):
```elixir
import EctoGettext
```
3. Create attributes.po and errors.po files in priv/gettext/_locale_/LC_MESSAGES/
```elixir
# attributes.po - file with Ecto attributes such as username, email, password, etc.
msgid "Username"
msgstr "Имя пользователя"
msgid "Password"
msgstr "Пароль"
```
```elixir
# errors.po - file with Ecto validation errors
# Please use only %{count} interpolation, because it is a hard rule
msgid "can't be blank"
msgstr "не может быть пустым"
msgid "should be at least %{count} characters"
msgstr "не может быть короче %{count} символов"
msgid "should be at most %{count} characters"
msgstr "не может быть длиннее %{count} символов"
msgid "has invalid format"
msgstr "имеет неверный формат"
```
4. Use it in your forms:
```elixir
# Example with slim templates
= for {attr, message} <- localize_validations(MyApp.Gettext, f.errors) do
li
= humanize(attr) <> " "
= message
```
### Also, you can localize attributes and messages separately:
1. For localize attributes:
```elixir
localize_attribute(MyApp.Gettext, :username)
```
It's can be useful in forms, for example:
```elixir
= form_for @changeset, registration_path(@conn, :create), fn f ->
= text_input f, :username, placeholder: localize_attribute(MyApp.Gettext, :username)
= email_input f, :email, placeholder: localize_attribute(MyApp.Gettext, :email)
...
```
2. For localize messages:
```elixir
localize_message(MyApp.Gettext, {"can't be blank", []})
```