Skip to content
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

Merged
merged 2 commits into from
Jun 13, 2022

Conversation

Senen
Copy link
Member

@Senen Senen commented Sep 8, 2021

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 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.

[1] https://guides.rubyonrails.org/i18n.html#managing-the-locale-across-requests

@Senen Senen self-assigned this Sep 8, 2021
@Senen Senen added this to Reviewing in Consul Democracy via automation Sep 8, 2021
@javierm javierm self-assigned this Sep 8, 2021
Copy link
Member

@javierm javierm left a 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 😉.

spec/spec_helper.rb Outdated Show resolved Hide resolved
app/controllers/application_controller.rb Outdated Show resolved Hide resolved
@javierm javierm moved this from Reviewing to Doing in Consul Democracy Sep 8, 2021
@Senen
Copy link
Member Author

Senen commented Sep 8, 2021

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?

Interesting conversation, thanks for sharing it. Yes, that's the idea! Using the before_filter to set the locale is not wrong as the method I18n.locale= is thread-safe, but as CONSUL forks can uses gems with engines I thought it's better to use the new suggested way.

@Senen
Copy link
Member Author

Senen commented Sep 8, 2021

Hi @javierm,

What do you think about changing the name of the set_locale controller methods to switch_locale as the Rails 6 documentation example? It's not a big deal, it is just to follow Rails guides as much as we can 🤔

@Senen Senen changed the title Enable the Rails/I18nLocaleAssignment cop to scan all the application ruby files. Enable the Rails/I18nLocaleAssignment cop to scan all the application ruby files Sep 8, 2021
@javierm
Copy link
Member

javierm commented Sep 8, 2021

@Senen No strong preference, so whatever you prefer is fine 👌.

@javierm javierm added the Linters Rubocop, ERB Lint, ESLint, SCSS-Lint, ... label Sep 8, 2021
@javierm javierm moved this from Doing to Reviewing in Consul Democracy Nov 27, 2021
@javierm javierm moved this from Reviewing to Doing in Consul Democracy Nov 27, 2021
@javierm javierm added the 1.5 label Jun 2, 2022
Use the around action to set the desired locale so we do not break the rule.
@Senen Senen force-pushed the set_request_locale branch 3 times, most recently from 066e09c to b848cbc Compare June 8, 2022 14:47
@Senen Senen moved this from Doing to Reviewing in Consul Democracy Jun 9, 2022
@javierm javierm moved this from Reviewing to Doing in Consul Democracy Jun 10, 2022
@Senen Senen moved this from Doing to Reviewing in Consul Democracy Jun 11, 2022
@javierm javierm moved this from Reviewing to Doing in Consul Democracy Jun 13, 2022
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
@Senen Senen moved this from Doing to Reviewing in Consul Democracy Jun 13, 2022
Consul Democracy automation moved this from Reviewing to Testing Jun 13, 2022
@Senen Senen merged commit ce918e7 into master Jun 13, 2022
Consul Democracy automation moved this from Testing to Release 1.5.0 Jun 13, 2022
@Senen Senen deleted the set_request_locale branch June 13, 2022 16:43
@javierm javierm removed the 1.5 label Jun 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Linters Rubocop, ERB Lint, ESLint, SCSS-Lint, ...
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants