class RuboCop::Cop::I18n::RailsI18n::DecorateStringFormattingUsingInterpolation
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.
@example
# bad t("status.#{status_string}") t("status." + "accepted")
@example
# good t("status.accepted")
Public Instance Methods
on_send(node)
click to toggle source
# 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
Private Instance Methods
contains_string_formatting_with_interpolation?(node)
click to toggle source
# File lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb, line 48 def contains_string_formatting_with_interpolation?(node) return node.any? { |n| contains_string_formatting_with_interpolation?(n) } if node.is_a?(Array) return string_contains_interpolation_format?(node.source) if node.respond_to?(:type) && (node.str_type? || node.dstr_type?) return node.children.any? { |child| contains_string_formatting_with_interpolation?(child) } if node.respond_to?(:children) false end
error_message(method_name)
click to toggle source
# File lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb, line 40 def error_message(method_name) "'#{method_name}' function, message key string should not contain \#{} formatting" end
string_contains_interpolation_format?(str)
click to toggle source
# File lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb, line 44 def string_contains_interpolation_format?(str) str.match(/\#{[^}]+}/) end