-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
#326
Conversation
c0b9a10
to
216afa2
Compare
216afa2
to
8b84e60
Compare
"#{path}.#{action_name}.#{key}" | ||
end | ||
|
||
def controller_path(controller) |
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.
Fantastic!
expect_no_offenses(<<~RUBY) | ||
class FooController | ||
def action | ||
t ['foo.action.key'] |
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 to I18n.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:
> ['.a', '.b'].start_with?('.')
NoMethodError: undefined method `start_with?' for [".a", ".b"]:Array
It would work before Rails 7, because it was implemented as:
if key.to_s.first == "."
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 😄
8b84e60
to
4bfbea5
Compare
Updated. |
4bfbea5
to
7092c04
Compare
7092c04
to
eed8c42
Compare
Updated. |
I will merge this new cop after the next bug fix release. Thank you. |
…of multiline Follow up to #326 (comment). For now this repository can be set to `SafeMultiline: false`.
Thanks! |
Implementation of this https://rails.rubystyle.guide/#lazy-lookup