Class: RuboCop::Cop::DevDoc::I18n::RequireTranslation

Inherits:
Base
  • Object
show all
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}"

Examples:

# bad
view.p text: 'Welcome'

# bad
view.fields_text label: 'Email'

# bad (nested hash prop)
view.icon tooltip: { text: 'Share' }

# bad (jbuilder positional)
json.title 'Forms'

# bad (interpolation, but no translation call)
view.p text: "Hi #{name}"

# good
view.p text: t('home.welcome')

# good (interpolates a translation call)
view.p text: "#{t('home.greeting')} #{user.name}"

# good (non-literal — not flagged)
view.p text: 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

Method Summary

Methods included from LocalizableProps

#on_send