Wednesday, May 25, 2011

Translations / internationalization for active record error messages

Active Record will look for messages in this order:

activerecord.errors.models.admin.attributes.title.blank
activerecord.errors.models.admin.blank
activerecord.errors.models.user.attributes.title.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.title.blank
errors.messages.blank

So example for validation for email format


# app/models/membership_requests.rb
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

# config/locales/en.yml
en:
activerecord:
errors:
models:
membership_request:
attributes:
email:
invalid: "is not a valid email address"




reference: http://guides.rubyonrails.org/i18n.html

No comments: