Let Cop/StaticTranslationDefinition detect use of `_(..)` in class context
Problem
Currently, Cop/StaticTranslationDefinition
does not flag the following code.
# BAD
class Record
validates :foo, message: _('An error')
end
Solution
Tweak Cop/StaticTranslationDefinition
to detect such cases as well and suggest to use proc { ... }
instead:
# GOOD
class Record
validates :foo, message: proc { _('An error') }
end
Implementation guide
Files to modify:
- rubocop/cop/static_translation_definition.rb
- spec/rubocop/cop/static_translation_definition_spec.rb
Alternative solution
Note that plain message:
is translated by via GetText as well and _(...)
doesn't need to be used. The following solution is equivalent:
# GOOD but implicit
class Record
validates :foo, message: 'An error'
end
From https://github.com/grosser/gettext_i18n_rails#activerecord---error-messages:
Error messages can be translated through FastGettext, if the ':message' is a translation-id or the matching Rails I18n key is translated.
Refs
Mentioned by @ahegyi during review of !74290 (comment 780455272)
See also https://docs.gitlab.com/ee/development/i18n/externalization.html#keep-translations-dynamic
Edited by Dan Jensen