New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make i18n locale setting docs use around_action #34356
Make i18n locale setting docs use around_action #34356
Conversation
8c2fed4
to
21b2aa0
Compare
Changes `I18n.locale` assignment in docs to use `I18n.with_locale` in `around_action` to ensure locale resetting after action processing. [ci skip] [Gannon McGibbon + Leonardo Tegon]
21b2aa0
to
bcccf8b
Compare
For the archives: This patch is not really about thread-safety. The writer Indeed, the whole edit is dubious to me. Requests set a locale with high priority. No matter if done in a The documentation used |
@fxn Thanks for clarifying. Yes, the docs now recommend a slightly more convoluted way of setting locales but I would argue its still better practice to reset the locale once you're done processing the controller action. As described in the issue, depending on the web server setup, locales can leak across requests. If I understand this whole problem correctly, an important thing to highlight is to make sure to set the locale on every request if you use |
I am about to take a plane, but don’t see how the server setup may lead to leakage. The setter is thread-safe, different controllers run in different threads. QED. From the PR description I believe the patch is motivated by a misunderstanding (and your colleague deserves being told their code was fine). |
It is not related to server setup, it is related to the application setup. The |
Two remarks: The documentation has the filter in If you have a bug and forget to set the user’s locale in a controller, you have a bug and using the previous one is as incorrect as using the default one. |
Hey, let me be clear. I don’t want to revert this patch. Don’t like this style but I respect the merge. My point is that the whole idea was to fix a threading issue that does not exist. If the patch is left, it has to be clear the reasoning is a different one. |
The second case I agree, it is a bug in the application. But the leaky locale in the application reaching out to engines is a real problem and I think using This problem, also happens with the ActiveSupport::Current but we do solve that by adding automatic reset of state when the I totally agree this is not fixing a threading issue. This is fixing a leaky state issue reaching out to controller mounted in the application using engine. That is a different issue of the one linked to this PR, but I still think it is worth to fix since most applications don't have control over their engines. |
Fair enough Rafael. This is optimizing for a very particular use case in which you have controllers out of your control, and that serve stuff not subject to locale, for which the default locale is fine. I can see that argument and no prob leaving this change on that basis 👍🏼. |
Since rails#34356 logging `locale` value is more correct
Summary
Makes examples in the
I18n
docs that switch locales in controllersthread-safeusearound_action
in order to be less susceptible to leakage.Closes #34043.