-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Enable the Rails/I18nLocaleAssignment
cop to scan all the application ruby files
#4696
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reading the comments at rails/rails#34356 and the commit messages from this pull request, if I understand correctly, we aren't changing the controllers in order to avoid leaks between requests, but in order to avoid the case where developers could set the locale somewhere else without realizing the possible consequences of doing so. Is that correct?
If that's the case, I'm fine with the changes 👌.
I've left a couple of comments as usual 😉.
Interesting conversation, thanks for sharing it. Yes, that's the idea! Using the |
e2aa2a9
to
ddd8504
Compare
Hi @javierm, What do you think about changing the name of the |
Rails/I18nLocaleAssignment
cop to scan all the application ruby files.Rails/I18nLocaleAssignment
cop to scan all the application ruby files
@Senen No strong preference, so whatever you prefer is fine 👌. |
ddd8504
to
405cd69
Compare
Use the around action to set the desired locale so we do not break the rule.
066e09c
to
b848cbc
Compare
b848cbc
to
f15dd38
Compare
This cop scans only the tests files by default, but we prefer to scan all application Ruby files, so when a developer uses the class method `I18n.locale=`, the cop will embrace using the method `I18n.with_locale` instead. By doing this way, the cop will help developers to avoid unexpected translation errors. Quoting the Rails 6 guides: > I18n.locale can leak into subsequent requests served by the same thread/process if it is not consistently set in every controller. For example executing I18n.locale = :es in one POST requests will have effects for all later requests to controllers that don't set the locale, but only in that particular thread/process. For that reason, instead of I18n.locale = you can use I18n.with_locale which does not have this leak issue. Now we enabled the cop for all application Ruby files; we have to remove the assignments at the controller level to set the request locale. As Rails 6 guides suggest [1], we can use the `around_action` controller callback to set each request locale without breaking the rule. This cop will warn CONSUL developers when using `I18n.locale` assignment embracing them to use the `I18n.with_locale`instead. [1] https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests
f15dd38
to
c263a6f
Compare
References
Discussion from PR #4629
Objectives
Enable the
Rails/I18nLocaleAssignment
cop to scan all application ruby files.This cop scans only the tests files by default, but we prefer to scan all application Ruby files, so when a developer uses the class method
I18n.locale=
, the cop will embrace using the methodI18n.with_locale
instead. By doing this way, the cop will help developers to avoid unexpected translation errors.Quoting the Rails 6 guides:
Now we enabled the cop for all application Ruby files; we have to remove the assignments at the controller level to set the request locale. As Rails 6 guides suggest [1], we can use the
around_action
controller callback to set each request locale without breaking the rule.[1] https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests