Reputation: 14913
I have a site thats served in 2 flavours, English and French. Here's some code
app/views/user/register.html.erb
-----------------
<% form_for .....>
<%= f.text_field :first_name %>
<% end %>
app/models/user.rb
------------------
class User < ActiveRecord::Base
validates_presence_of :first_name
end
Now to display the error message in case if the site is being served in the French version, I have
app/config/locales/fr.yml
-------------------------
activerecord:
errors:
messages:
empty: "ne peut pas être vide"
So if someone does not fill in a first name, the validator takes the name of the field and appends the custom message for empty clause giving
"First name ne peut pas être vide"
which is incorrect, coz 'First name' in French is 'Prénom', hence it should be
"Prénom ne peut pas être vide"
Please can someone suggest a way of achieving the desired result.
Upvotes: 2
Views: 2629
Reputation: 7388
You can use this gem: advanced_errors
to specify custom error messages that do not include the field name if the first character is '^'. So, you'll have messages like
"^Prénom ne peut pas être vide"
and they'll show normally.
Upvotes: 0
Reputation: 44952
From the Rails documentation for generate_full_methods in the ActiveRecord::Error class...
Wraps an error message into a full_message format.
The default full_message format for any locale is "{{attribute}} {{message}}". One can specify locale specific default full_message format by storing it as a translation for the key :"activerecord.errors.full_messages.format".
Additionally one can specify a validation specific error message format by storing a translation for :"activerecord.errors.full_messages.[message_key]". E.g. the full_message format for any validation that uses :blank as a message key (such as validates_presence_of) can be stored to :"activerecord.errors.full_messages.blank".
Because the message key used by a validation can be overwritten on the validates_* class macro level one can customize the full_message format for any particular validation:
# app/models/article.rb class Article < ActiveRecord::Base
validates_presence_of :title, :message => :"title.blank" end #
config/locales/en.yml en:
activerecord: errors: full_messages: title: blank: This title is screwed!
Upvotes: 6