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

前端 未结 2 593
鱼传尺愫
鱼传尺愫 2021-02-01 20:31

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         


        
2条回答
  •  春和景丽
    2021-02-01 20:47

    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.

提交回复
热议问题