Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:01:15+00:00 2026-06-16T01:01:15+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:01:16+00:00Added an answer on June 16, 2026 at 1:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a controller that will respond to /favicon.ico appropriately. But I just realized
I just realized that I don't see a way to collect a string from
I just realized that this piece of code works well in Firefox but not
I just realized that the MySQLWorkBench destroys the target (makes a replacement) when performing
I just realized that I need to synchronize a significant amount of data collection
I just realized that the method Element.getElementsByTagName(someTagName) returns a nodelist of all elements in
I just realized that i may not be following best practices in regards to
I just realized that when i start a task from within a task and
I just realized that my app, with over 300 users still using an Android
I've just realized that when I use write.table() for saving a data frame in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.