I recently received a great pull request for Keila, my Open Source email newsletter tool, that added a French translation. Even though I had previousl

Translating Ecto Changeset Errors in Phoenix

submited by
Style Pass
2025-01-22 14:00:08

I recently received a great pull request for Keila, my Open Source email newsletter tool, that added a French translation. Even though I had previously translated Keila to German, it seems like I missed quite a few strings that should have been translated. One of the aspects I had previously neglected to translate were Ecto changeset errors from custom validations.

So in this how-to I’ll share what I learned when fixing this and how you can translate Ecto changeset errors in your own Phoenix applications.

Every new Phoenix app comes with some macros based on the the Gettext library. Theese macros make translating an application quite easy.

If you want to translate a string in a Phoenix view, simply wrap it with the gettext() macro like this: <%= gettext("Translate me!") %> and it’s now translatable! If you want to support pluralization, you can use ngettext("an apple", "%{count} apples", n) instead.

Thus, it might seem like a no-brainer to just import MyAppWeb.Gettext in your schema module and use gettext/2 or ngettext/4 directly in a custom validation like so:

Leave a Comment