Class: RuboCop::Cop::Rails::ShortI18n

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/rails/short_i18n.rb

Overview

Enforces that short forms of I18n methods are used: t instead of translate and l instead of localize.

This cop has two different enforcement modes. When the EnforcedStyle is conservative (the default) then only I18n.translate and I18n.localize calls are added as offenses.

When the EnforcedStyle is aggressive then all translate and localize calls without a receiver are added as offenses.

Examples:

# bad
I18n.translate :key
I18n.localize Time.now

# good
I18n.t :key
I18n.l Time.now

EnforcedStyle: conservative (default)

# good
translate :key
localize Time.now
t :key
l Time.now

EnforcedStyle: aggressive

# bad
translate :key
localize Time.now

# good
t :key
l Time.now

Constant Summary collapse

MSG =
'Use `%<good_method>s` instead of `%<bad_method>s`.'
PREFERRED_METHODS =
{ translate: :t, localize: :l }.freeze
RESTRICT_ON_SEND =
PREFERRED_METHODS.keys.freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/rails/short_i18n.rb', line 55

def on_send(node)
  return if style == :conservative && !node.receiver

  long_i18n?(node) do |method_name|
    good_method = PREFERRED_METHODS[method_name]
    message = format(MSG, good_method: good_method, bad_method: method_name)
    range = node.loc.selector

    add_offense(range, message: message) do |corrector|
      corrector.replace(range, PREFERRED_METHODS[method_name])
    end
  end
end