Question

In our rails app. it 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? Many thanks.

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top