Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why locale setting in Rails acts as global (when using Thin)?

I just realized that the recommended Rails way to set locale in your controller

before_filter :set_locale

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end

sets the locale globally. The code above works, but I wonder is default_locale really default if you have to type it explicitly?

What I'd expect is to have a locale per request (like we have session per request) and doing something like:

def set_locale
  locale = params[:locale] if params[:locale]
end

And having I18n.default_locale used by default otherwise. This would match ideally the optional locale in path:

# config/routes.rb
scope "(:locale)", :locale => /en|nl/ do
  resources :books
end

For now if for some reason I skip locale setting in some action it uses the locale set in the previous request which could be from another user!

And isn't there a potential race condition as one request can change global I18n.locale while another request (having set another locale beforehande) is in the middle of rendering?


UPDATE: Some details I found for now, from the I18n documentstion:

Sets the current locale pseudo-globally, i.e. in the Thread.current hash def locale=(locale)

Now I want to understand if every request is a separate thread.


UPDATE 2: See my answer for explanation.

like image 511
khustochka Avatar asked Feb 13 '12 15:02

khustochka


2 Answers

So now the final answer. TL;DR Setting locale acts as global only when you use threaded web servers, like Thin and Puma.

As I mentioned, I18n.locale=

Sets the current locale pseudo-globally, i.e. in the Thread.current hash

So it is supposed to be per-request, and it works this way in Webrick and Unicorn.

But if you use threaded web server like Thin or Puma, seems that the thread lives longer, and the value is preserved for future requests, until it is changed explicitly. Where I learned it is from the new Steve Klabnik's gem request_store:

If you need global state, you've probably reached for Thread.current.

<...>

So people are using those fancy threaded web servers, like Thin or Puma. But if you use Thread.current, and you use one of those servers, watch out! Values can stick around longer than you'd expect, and this can cause bugs.

like image 124
khustochka Avatar answered Nov 18 '22 15:11

khustochka


Recommended code from above does not set locale globally it sets it by request.

before_filter :set_locale

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end

Code is usually place in BaseController so before each page is render it is triggered and set. There is no race conditions since every page will trigger this code and I18n locale will be calculated there. You can expand this to let's say looks for users locale, than session locale, than request params, than uses English.

def set_locale
  I18n.locale = @user.locale || session[:locale] || params[:locale] || :en
end

In other words if you set local on one page let's say in home controller to german and got to dashboard controller you will see default language (english). Since change is not global. That is why code is placed in base controller. Hope it makes sense.

like image 23
Haris Krajina Avatar answered Nov 18 '22 13:11

Haris Krajina