Class: RuboCop::Cop::I18n::RailsI18n::DecorateStringFormattingUsingInterpolation

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb

Overview

When using an decorated string to support I18N, any strings inside the decoration should not contain the ‘#{}’ interpolation string as this makes it hard to translate the strings.

Examples:


# bad

t("status.#{status_string}")
t("status." + "accepted")

# good

t("status.accepted")

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb', line 24

def on_send(node)
  return unless node&.loc&.selector

  decorator_name = node.loc.selector.source
  return unless RailsI18n.supported_decorator?(decorator_name)

  method_name = node.method_name
  arg_nodes = node.arguments
  return unless !arg_nodes.empty? && contains_string_formatting_with_interpolation?(arg_nodes)

  message_section = arg_nodes[0]
  add_offense(message_section, message: error_message(method_name))
end