Search code examples
rubyruby-on-rails-3internationalizationrails-i18n

How to set I18n.t in Rails 3.2 for individual user?


Our rails app sets the I18n.t in a file under config/initializers:

I18n.default_locale = 'EN' if Rails.env.production?

This line of code is executed before a user logs in. We would like to set the I18n.t environment based on individual user after she/he logs in. Is there a way doing this individual user based I18n.t?


Solution

  • The the I18n.locale in a before_filter in your application_controller after you identified the current_user.

    before_filter :set_locale
    
    private
    def set_locale
      I18n.locale = current_user.preferred_locale if current_user
    end
    

    Note: You might need to change current_user and preferred_locale to match the naming of these instances and attributes in your application.

    See Rails guides about setting and passing the locale.