Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Add new
Rails/I18nLazyLookup
cop #326Add new
Rails/I18nLazyLookup
cop #326Changes from all commits
eed8c42
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Why is this only for controllers? How about mailers, models, serializers etc?
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.
If I remember correctly, "Lazy Lookup" works only inside views and controllers? https://guides.rubyonrails.org/i18n.html#lazy-lookup
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.
Fair enough.
Sadly, the guide confusingly tells "Rails implements a convenient way to look up the locale inside views".- edit they also tell "can also be used in controllers", so it's fine.Views are unlikely something we can parse with RuboCop, can we?
So it's all good here 👍
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.
I haven't seen that rubocop has any view checking code in any cops, so do not know.
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.
Is this legal?
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.
Rails'
t
helpers delegate toI18n.translate
and this is used for bulk translates.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.
Tested this and found a couple of notable things:
t ['.key']
won't work as doesn't Rails allow lazy lookup for bulk lookup. So this example in question is perfectly fine.What troubled me is the implementation of
translate
in controller code:It would work before Rails 7, because it was implemented as:
where
key
is['.a', '.b']
, but I suspect it would fail in Rails 7.AbstractController::Translation
is not included toActionController::API
, only toActionController::Base
](https://github.com/rails/rails/blob/main/actionpack/lib/action_controller/base.rb). It doesn't make the cop unsafe, since it's not us who suggest usingt
, it's already there in the code.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.
Thanks for investigation 👍
Yes, I think no one actually uses this bulk feature, since, as you mentioned, it doesn't work.
This test actually tests that no offenses are generated for this case. Only when the keys is a string or a symbol.
So, wdyt, should I remove this test case or left it as is?
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.
It's fine to keep it, just to make sure we don't add to the existing mess 😄