Class: RuboCop::Cop::DevDoc::I18n::AvoidTitleizeHumanize

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/dev_doc/i18n/avoid_titleize_humanize.rb

Overview

Warn when .titleize or .humanize is used to derive display text.

Rationale

titleize/humanize turn an identifier into an English phrase ('sign_up'.titleize # => 'Sign Up'). When that result is shown to a user it bypasses the I18n catalog and can only ever read as English — it can't be translated. Localize the text with a t(...) lookup instead of transforming a symbol/column name at render time.

This can't tell a display string from one used to build a key or a log line, so it is a review aid: disabled by default, warning severity, and scoped to view files. Run it during a localization pass.

⚠️ English-only — can't be translated
view.h1 text: status.titleize
view.p text: user.role.humanize

✔️ Resolved through I18n
view.h1 text: t("status.#{status}")

Examples:

# warning
view.h1 text: status.titleize

# warning
view.p text: model_name.humanize

# good
view.h1 text: t("status.#{status}")

Constant Summary collapse

MSG =
'Avoid `.%<method>s` for display text: the result is ' \
'English-only and bypasses I18n. Use `t(...)` instead.'.freeze
DEFAULT_METHODS =
%w[titleize humanize].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/dev_doc/i18n/avoid_titleize_humanize.rb', line 40

def on_send(node)
  return unless forbidden_methods.include?(node.method_name.to_s)

  add_offense(
    node.loc.selector,
    message: format(MSG, method: node.method_name)
  )
end