Glot.Ecto (Glot v0.1.1)

View Source

Provides the hint/2 function for translating custom Ecto validation messages using a localized glossary.

Designed for use in modules that define or consume Ecto.Changeset validations.

Why?

Ecto allows adding errors with custom messages and metadata:

add_error(
  changeset,
  :field,
  "you're doing it wrong",
  foo: "bar",
  validation: :foobar
)

Later, you can retrieve that message from the changeset:

{"you're doing it wrong", [foo: "bar", validation: :foobar]}

Glot.Ecto.hint/2 takes this {message, opts} tuple along with a locale and returns a localized, interpolated string — or falls back to the original.

Usage

defmodule MyApp.Validation do
  use Glot.Ecto, ["../locales/validation"]
end

MyApp.Validation.hint({"you're doing it wrong", [foo: "bar", validation: :foobar]}, "en")
#=> "you're doing bar wrong"

Source format

The glossary is built from JSONL files like this:

# validation.en.jsonl
{"key": "foobar", "value": "you're doing {{foo}} wrong"}

# validation.ru.jsonl
{"key": "foobar", "value": "вы неправильно делаете {{foo}}"}

File paths passed to use Glot.Ecto should be base paths without extension or locale suffix.