Skip to content

Cop/StaticTranslationDefinition: Check multiline strings

What does this MR do and why?

To support multiline strings we need to allow dynamic strings as well. Both are dstr.

With this, we check for:

  _('long string ' \
    'ends here')

In Ruby AST it's a dstr but later it's just a plain string 'long string ends here' and thus normally used by gettext.

Now, we also check for:

  _("something with #{interpolation}")

This is because it's also dstr from AST point of view. Interpolated strings are not recognized by gettext. This fact we have to catch in a separate cop.

Found while investigating for #374091 (closed).

How to set up and validate locally

  1. Apply the following patch to simulate an offense with multiline strings:
diff --git a/app/controllers/concerns/verifies_with_email.rb b/app/controllers/concerns/verifies_with_email.rb
index d00952015e1..fee79a60d47 100644
--- a/app/controllers/concerns/verifies_with_email.rb
+++ b/app/controllers/concerns/verifies_with_email.rb
@@ -102,6 +102,11 @@ def verify_token(user, token)
     end
   end
 
+  def self.m
+    @m ||= s_('IdentityVerification|Maximum login attempts exceeded. '\
+      'Wait %{interval} and try again.') % { interval: user_sign_in_interval }
+  end
+
   def render_sign_in_rate_limited
     message = s_('IdentityVerification|Maximum login attempts exceeded. '\
       'Wait %{interval} and try again.') % { interval: user_sign_in_interval }

On master 💥

No offense found although expected 💥

bundle exec rubocop -C false --only Cop/StaticTranslationDefinition app/controllers/concerns/verifies_with_email.rb

Inspecting 1 file
.

1 file inspected, no offenses detected

On MR

Offense found

$ bundle exec rubocop -C false --only Cop/StaticTranslationDefinition app/controllers/concerns/verifies_with_email.rb
Inspecting 1 file
C

Offenses:

app/controllers/concerns/verifies_with_email.rb:106:12: C: Cop/StaticTranslationDefinition: Translation is defined in static scope. Keep translations dynamic. See https://docs.gitlab.com/ee/development/i18n/externalization.html#keep-translations-dynamic
    @m ||= s_('IdentityVerification|Maximum login attempts exceeded. '\ ...
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Peter Leitzen

Merge request reports