Class: RuboCop::Cop::I18n::GetText::DecorateStringFormattingUsingInterpolation

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/i18n/gettext/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.

Check GetText.supported_decorators for a list of decorators that can be used.

Examples:


# bad

_("result is #{this_is_the_result}")
n_("a string" + "a string with a #{float_value}")

# good

_("result is %{detail}" % {detail: message})

Instance Method Summary collapse

Methods inherited from Cop

#highlights, #messages

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  decorator_name = node.loc.selector.source
  return unless GetText.supported_decorator?(decorator_name)

  _, method_name, *arg_nodes = *node
  if !arg_nodes.empty? && contains_string_formatting_with_interpolation?(arg_nodes)
    message_section = arg_nodes[0]
    add_offense(message_section, message: "'#{method_name}' function, message string should not contain \#{} formatting")
  end
end