breaking into the unknown…

customize error message with locales

Leave a comment

locales allow you to add multiple language to your application. It is based on rails I18n API , the i18n wiki is available here . From rails 2.2 onward the I18n API is bundled as a dependency gem for rails. It is always a good practice to separate out, constant stuffs exposed to user ,  like validation error messages , flash notices etc in locales. If you structure your code like this from beginning, it will be easy for you to display the message in any language, the default is English. So by default rails will load all the messages from en.yml file. If say your site is in Germany and you want to show the validation message in German language. Then you will write all the validation message in de.yml file and set the default language to :de in application.rb file with the below line

config.i18n.default_locale = :de

So now rails will try to look for the message in de.yml file. At any point, you can make rails to load a specific language file any time by passing locales parameter in URL

Example : # it will load all the locales from 
en.yml extensions like devise.en.yml, rest.en.yml etc # it will load all the locales from 
de.yml extensions like, etc

The best way is to provide the extension in domain name itself


Above explanation is just a Introduction of locales and I will not get into more detail here. You may read the reference provided below. Let us move our validation error message to locals.

Below, is the current validation

class User < ActiveRecord::Base
validates :source_system, :source_system_id
validates_uniqueness_of :source_system_id, :message => "ID has already been taken"
validates :entity_type, :inclusion =>  {:in => %w(Customer User), :message => "should be Customer or User"}
validates :target_system, :inclusion =>  {:in => %w(LOCAL OPENAM), :message => "should be LOCAL or OPENAM"}

We can move these validation message to en.yml file locales. I have also shown providing custom name to the model and its attributes.

But let us first remove the message from the user model, we do not need them as we are now setting the message in the locals

class User < ActiveRecord::Base
validates :source_system, :source_system_id
validates_uniqueness_of :source_system_id
validates :entity_type, :inclusion =>  {:in => %w(Customer User)}
validates :target_system, :inclusion =>  {:in => %w(LOCAL OPENAM)}

Below is the code in config/locales/en.yml file

      user: "Customer"
        email: "Email address"
              blank: "must fill %{attribute}" 
              taken: " Id User already provisioned"
              inclusion: "should be Customer or User"
              inclusion: "should be LOCAL or OPENAM"

Note, that the validation to be used in locals is not exactly same as we use in model, for example in model we have checked uniqueness for source_system_id, but in local above we have used :taken, similarly when we use :presence => true in model, in locals we used blank. Also see the way we have used attribute in the message, thus the message will become must fill source_system_id.

The complete mapping of active record validation with corresponding validation to be used in locals is as below.

list of ActiveRecord Validation maped to corrosponding validation to be used in locals

list of ActiveRecord Validation mapped to corresponding validation to be used in locals


Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s