Translate Ecto validation messages

If you used mix phoenix.new generator, it already created the required functions and infrastructure for you. If you open your generated error_helpers.ex you will find translate_error/1 function there that does what you want. You can use it directly in your views/templates (or import it in other places if you need to move to different layer). It takes the tuple with string and options, which is exactly what Ecto validations return.

Here is the template for reference:

Please note that if you do use the error_tag/2 function from the same module, it already translates the errors for you.

You should already have the translations template file in priv/gettext/errors.pot, and you can put in the translations you want to say priv/gettext/fr/LC_MESSAGES/errors.po

3 Likes