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

Add a cop to enforce internationalization #624

Closed
pirj opened this issue Jan 12, 2022 · 4 comments · Fixed by #643
Closed

Add a cop to enforce internationalization #624

pirj opened this issue Jan 12, 2022 · 4 comments · Fixed by #643
Labels
feature request Request for new functionality

Comments

@pirj
Copy link
Member

pirj commented Jan 12, 2022

#620 brought up a problem with user-facing string appearing directly in models.

class Foo < ApplicationRecord
  belongs_to :bar
  validates :bar, presence: { message: 'show me the way to the next whiskey bar' }
end

Describe the solution you'd like

Add a cop that enforces https://rails.rubystyle.guide/#locale-texts, at least for basic cases for the start.

No strings or other locale specific settings should be used in the views, models and controllers. These texts should be moved to the locale files in the config/locales directory.

@koic koic added the feature request Request for new functionality label Jan 14, 2022
@fatkodima
Copy link
Contributor

@pirj How do you think this will be better implemented? Just checking for strings in models/controllers/etc would give a lot of false positives.

So this should be implemented as checking specific methods and their arguments? Like validates messages, redirect_to notices/alerts/errors, mailers subjects, etc?

@pirj
Copy link
Member Author

pirj commented Feb 5, 2022

Exactly.

@andyw8
Copy link
Contributor

andyw8 commented Feb 5, 2022

I think this would be a useful cop, but should be disabled by default. The majority of apps only target a single locale, and this approach requires extra effort.

(and I would support updating the styleguide to reflect this nuance).

@pirj
Copy link
Member Author

pirj commented Feb 5, 2022

this approach requires extra effort

Fair enough. Some thoughts aloud.

Even though the app might be a single locale, it's easier for non-tech staff to edit a singe YAML file to fix copy rather than update code. I've seen this in practice a couple of times. Surely, adding new entries to model validation will still require developer's effort, due to a non-trivial structure, but updating existing non-default ones is possible.

When the cop is disabled by default, it's nearly ever used. Some cops just don't apply in certain cases, and it's a usual routine to disable them on a new project, e.g. Metrics/BlockSize for specs/**/*.rb.

It reminds me of "the business logic should naturally reside in the model" and the "keep complicated formatting logic out of views". It's tempting to have logic in controllers and views, and putting it to where it belongs is an extra effort, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for new functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants