Hello, dear!
I am writing a website, and there is a need for its internationalization. I did everything as written on rusrails.ru , but the language changes only when I change the locale parameter from en to ru in the address bar of the GET. I also need to link to another language in the corner of the page.
A bit of surfing the Internet found a post on stackoverflow.com , here's the link - Change locale at runtime in Rails 3 .
Following this example, I made several methods in my controllers:
ApplicationController.rb

before_filter :set_locale def set_locale if current_user I18n.locale = current_user.locale else I18n.locale = params[:locale] || I18n.default_locale end end 

UsersController.rb

 def change_locale locale = params[:locale] raise 'unsupported locale' unless ['ru', 'en' ].include?(locale) if current_user current_user.locale = locale current_user.save end I18n.locale = locale redirect_to :index end 

In the view, I call the method like this:

 <%= link_to "English", :controller => 'users', :action => 'change_locale', :locale => 'en' %> 

Everything seems to be logical and should work, but unfortunately not everything works. When the user is not authenticated, that is, he does not log in, the link works, and the language switches perfectly, but when he logs in and tries to change the language, nothing happens. I decided to see whether the locale after the lines in the database is preserved:

 if current_user current_user.locale = locale current_user.save end 

and noticed that this was not happening, why I do not know.
I ask for help from the knowledgeable. Thank you in advance!

    2 answers 2

    Try updating the record like this:

    UsersController.rb

     private def change_locale locale = params[:locale] raise 'unsupported locale' unless ['ru', 'en'].include?(locale) User.find(current_user.id).update_attribute(:locale, locale) if current_user && !current_user.blank? I18n.locale = locale redirect_to :index end 
    • LTD!!! It's just great and just what you need! Thank you very much :) - MikroFF
    1. If the user chose the wrong locale, I think it is worthwhile to return not unsupported, but at least default_locale.

    2. How about storing a locale in a session?

       def set_locale I18n.locale = session[:locale] end 
    3. And if you still need to save the locale for an authorized user, you need to make sure that the user has such a field.

    4. In the end, I would do this:

       def set_locale I18n.locale = current_user.locale if current_user end 
    • Login made by skrinkastam Ryan Bates. You correctly understood, I want each user to have a field in the database with his locale, that is, when he logged in, his locale was accepted. And the most amazing thing is that it works for me, I implemented it. When a user logs in, a locale is added to him, like this: def create @user = User.new (params [: user]) @ user.locale = I18n.locale ... and when he logs in, the locale is accepted as I showed in the controller ApplicationController.rb. But I can not change the user locale when he already logged in. - MikroFF
    • How am I? You probably did not notice that I have this line in the ApplicationController.rb controller, it is simply framed by the if else operator - MikroFF