Reputation: 18064
How can ActiveRecord full error messages be translated? For example, I want to show them in spanish.
I create this file config/locales/es.yml
:
es:
errors:
attributes:
email:
blank: "El email no puede estar en blanco"
But when submitting a form with a presence: true
, validation is including the attribute always at the beginning of the message:
Email El email no puede estar en blanco
The first "Email" word is not necessary. How can I get rid of it?
Upvotes: 6
Views: 3944
Reputation: 139
I have the similar problem using Devise and Rails. What worked for me was:
<% resource.errors.values.each do |message| %>
<%= message[0] %>
<% end %>
Upvotes: 0
Reputation: 9840
Instead of including the field name in the error message and having to translate each message for each field, you can provide translations for the generic messages and the field names themselves. The generated config/locales/en.yml has a link to more translations: https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale Use the appropriate file(s) downloaded from there and provide translations for field names as demonstrated in the Rails I18n guide: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
See this answer for an example: https://stackoverflow.com/a/2859275/18038
Upvotes: 3
Reputation: 18064
Ok, after much reading and re-reading the official i18n guide, I discovered in the section 5.2.2 this:
ActiveModel::Errors#full_messages prepends the attribute name to the error message using a separator that will be looked up from errors.format (and which defaults to "%{attribute} %{message}").
So, the solution is to configure the format, like this:
es:
errors:
format: "%{message}"
attributes:
email:
blank: "El email no puede estar en blanco"
Upvotes: 10