Shiny happy people coding

Coding with smile

Gettext With Rails Can Be Long in an Error Form

| Comments

Today, I spend all the day try understand a problem in my application, develop in my new job. This bug is very simple. During the form validation, if it's not validate by rails, my controller render the same action with field in error. Normally, it's simple. But in my case, the page spend more 40 second to render. After read log, the problem is not in controller, but in my rendering page.

To understand all, I create a profil to my request with the script /script/performance/request (after a fight against). Once the profil create, I analyse that my problem was a search of translate error message that process a lot of. After a long time to search, I found the problem.

In rails, during the tag creation with helper method, there are a call to errors.on to know where is the error and highlight this field. But where in rails there are just a call to hash, Gettext has alias method and search in each call the translation. In production, this call is light because all translation cached. But in developpment mode, there are no cache. So each time, Gettext load the mo file and iterate on all translation string to find the good translation. It's this iteration that spend a lot of time and explain the long time to wait the rendering.

This behaviour is only in developpment environment, because there are test on this environment name. In all environment, all translation cached.

To resolv this problem, you can change the default environement with another name like dev otherwise development. In this case, all translation cached. You can alsa write this code GetText.cached = true in your envionnement.rb. But with this configuration, when you update mo file, you need restart your serveur to obtains translate entries.

Traduction fran├žaise