Specify translatable attributes
crbelaus opened this issue · 2 comments
When adding convencience functions to a translatable model (by using Trans module) it would be nice to explictly specify which attributes are translatable, and enforce the existence of those attributes in the changeset.
I am thinking about something like this (observe the fields
opt being passed to use Trans
)
defmodule Trans.Article do
use Ecto.Schema
use Trans, fields: [:title, :body], container: :translations
import Ecto.Changeset
...
end
About the validation, I am thinking about adding a new function to the model that checks if provided translations match the specified fields passed before. This function should be called explicitly on the desired changeset.
If we have translatable attributes specified on compile time, some interesting posibilities emerge:
- We could add convenience functions to retrieve each attribute. For example, if we translate the attribute
title
we could add aArticle.title/2
function which would receive an article and a locale, and return the title of the article translated in the given locale (this would be idiomatic Ruby, but I suspect that it would not be idiomatic Elixir) - We can enforce attributes on
Trans.Translatable.translate/3
function. If we only translatetitle
attribute, and are being asked for thefake
attribute we could return an informative error.
We will use a translates
key, which contains the list of translatable attributes for the model. An optional defaults
key can also be specified and will contain the default options that will be automatically passed to the convenience functions.