Class: RuboCop::Cop::DevDoc::I18n::RequireTranslation
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::DevDoc::I18n::RequireTranslation
- Includes:
- LocalizableProps
- Defined in:
- lib/rubocop/cop/dev_doc/i18n/require_translation.rb
Overview
Flag hardcoded user-facing strings in glib JSON-UI component props.
Rationale
Glib components render text from props like text:, label:, and
title:. Passing a string literal ships untranslatable copy — it can
never be localized and bypasses the I18n catalog. Pass t('...') (or
any non-literal) so the text resolves through the locale files.
Hash props (view.p text: '...'), nested hash props
(view.icon tooltip: { text: '...' }) and the jbuilder positional
form (json.title '...') are all checked. Empty/whitespace strings
are ignored — they carry no user-facing text. An interpolated string
("Hi #{name}") is flagged because its literal portions are still
hardcoded copy — unless it interpolates a t(...) (or
translate/I18n.t) call, which is treated as positive localization
and left alone.
The watched method names and localizable keys are configurable via
WatchedMethods: and LocalizableKeys:.
❌ Hardcoded — can't be translated
view.p text: 'Welcome'
view.fields_text label: 'Email'
view.icon tooltip: { text: 'Share' }
json.title 'Forms'
✔️ Resolved through I18n
view.p text: t('home.welcome')
view.fields_text label: t('user.email')
view.p text: "#{t('home.welcome')}, #{user.name}"
Constant Summary collapse
- MSG =
'Localize this string: pass `t(...)` instead of a hardcoded ' \ 'string for `%<key>s:`.'.freeze
Constants included from LocalizableProps
LocalizableProps::DEFAULT_LOCALIZABLE_KEYS, LocalizableProps::DEFAULT_WATCHED_METHODS, LocalizableProps::TRANSLATION_METHODS