Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
doubt about I18n || initializers
4 Posts
doubt about I18n || initializers

I'm using a rails application in portuguese language, and I put my translations on Rails.root + '/config/locales/ptbr.yml' I wrote the locale initializer on Rails.root + '/config/initializers/locale.rb' with:

I18n.locale = 'ptbr'

Testing on console works right but when mongrel start I see english messages again. After this I put on ApplicationController

before_filter :set_user_language


def set_user_language

I18n.locale = 'ptbr'


and it's works right! why I need to put like before_filter on ApplicationController and Rails initializers doesn't include on mongrel start!?

I thought that the environment should be the same in console and starting the web server.

Without a chance to look into this in detail my knee-jerk response is to wonder if this is a class reloading issue. Do you get the same problems if you turn class reloading off in development.rb?

I think you've misunderstood the I18n module.

First you MUST known that codes in "initializer" will be loaded ONLY ONCE while starting up your application. So if you put your translations under this folder it was initialized, works right in console without a HTTP request through controller module.

However, I18n module works like the following steps:

load all translations into application memory while starting up

after receiving a request, you should told your app to switch to the correct locale file

I18n reads all translations required in the template file for response

render the response body

So you should assign the locale for EACH request, since they will be assigned in different language, right? Before filter is the perfect place to handle these code.

If u are using rails 2.3.2, so the correct command to enable the internationalization is

config.i18n.default_locale = 'pt-BR'

Remember yet that u need have a pt-BR.yml file in config/locales. This file has a pre-determined structure.

I hope this can be helpful.

Diego Roriz

4 Posts
Login to add your message