]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Ruby: Improve heuristic for format strings.
authorBruno Haible <bruno@clisp.org>
Sun, 1 Jun 2025 23:36:07 +0000 (01:36 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 2 Jun 2025 00:22:55 +0000 (02:22 +0200)
* gettext-tools/src/format-ruby.c (struct spec): Add field
'likely_intentional_directives'.
(format_parse): Set it to the number of directives that don't contain a space.
(format_is_unlikely_intentional): New function.
(formatstring_ruby): Use it.
* gettext-tools/tests/format-ruby-3: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.

gettext-tools/src/format-ruby.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/format-ruby-3 [new file with mode: 0755]

index b26922f6402c589bd18537303df2e4db25aaa36f..e58e6277374ba65c5fdbeb671c9ef975ae8e9001 100644 (file)
@@ -98,6 +98,11 @@ struct numbered_arg
 struct spec
 {
   unsigned int directives;
+  /* We consider a directive as "likely intentional" if it does not contain a
+     space.  This prevents xgettext from flagging strings like "100% complete"
+     as 'ruby-format' if they don't occur in a context that requires a format
+     string.  */
+  unsigned int likely_intentional_directives;
   unsigned int named_arg_count;
   unsigned int numbered_arg_count;
   struct named_arg *named;
@@ -157,6 +162,7 @@ format_parse (const char *format, bool translated, char *fdi,
   struct spec *result;
 
   spec.directives = 0;
+  spec.likely_intentional_directives = 0;
   spec.named_arg_count = 0;
   spec.numbered_arg_count = 0;
   spec.named = NULL;
@@ -183,6 +189,8 @@ format_parse (const char *format, bool translated, char *fdi,
 
         enum format_arg_type type;
 
+        bool likely_intentional = true;
+
         FDI_SET (format - 1, FMTDIR_START);
         spec.directives++;
 
@@ -207,6 +215,8 @@ format_parse (const char *format, bool translated, char *fdi,
                     FDI_SET (format, FMTDIR_ERROR);
                     goto bad_format;
                   }
+                if (*format == ' ')
+                  likely_intentional = false;
                 format++;
                 continue;
               }
@@ -697,6 +707,8 @@ format_parse (const char *format, bool translated, char *fdi,
               }
           }
 
+        if (likely_intentional)
+          spec.likely_intentional_directives++;
         FDI_SET (format, FMTDIR_END);
 
         format++;
@@ -850,6 +862,14 @@ format_get_number_of_directives (void *descr)
   return spec->directives;
 }
 
+static bool
+format_is_unlikely_intentional (void *descr)
+{
+  struct spec *spec = (struct spec *) descr;
+
+  return spec->likely_intentional_directives == 0;
+}
+
 static bool
 format_check (void *msgid_descr, void *msgstr_descr, bool equality,
               formatstring_error_logger_t error_logger, void *error_logger_data,
@@ -976,7 +996,7 @@ struct formatstring_parser formatstring_ruby =
   format_parse,
   format_free,
   format_get_number_of_directives,
-  NULL,
+  format_is_unlikely_intentional,
   format_check
 };
 
index 43c0521c9881e48dea30884e3e5749a1b53589a2..7958785180d19e93b8498327514d54b3b81869d1 100644 (file)
@@ -223,7 +223,7 @@ TESTS = gettext-1 gettext-2 \
        format-perl-mixed-1 format-perl-mixed-2 \
        format-qt-1 format-qt-2 \
        format-qt-plural-1 format-qt-plural-2 \
-       format-ruby-1 format-ruby-2 \
+       format-ruby-1 format-ruby-2 format-ruby-3 \
        format-rust-1 format-rust-2 \
        format-scheme-1 format-scheme-2 \
        format-sh-1 format-sh-2 \
diff --git a/gettext-tools/tests/format-ruby-3 b/gettext-tools/tests/format-ruby-3
new file mode 100755 (executable)
index 0000000..320e129
--- /dev/null
@@ -0,0 +1,32 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test heuristic recognition of Ruby format strings like "100% complete".
+
+cat <<\EOF > f-r-3.rb
+gettext("Test 1a is 100% complete");
+gettext("Test 2a from 0% complete to 100% complete");
+gettext("Test 3a of %s is 100% complete");
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d f-r-3.tmp f-r-3.rb || Exit 1
+LC_ALL=C tr -d '\r' < f-r-3.tmp.po > f-r-3.po || Exit 1
+
+cat <<\EOF > f-r-3.ok
+msgid "Test 1a is 100% complete"
+msgstr ""
+
+msgid "Test 2a from 0% complete to 100% complete"
+msgstr ""
+
+#, ruby-format
+msgid "Test 3a of %s is 100% complete"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} f-r-3.ok f-r-3.po
+result=$?
+
+exit $result