]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: JavaScript: Improve heuristic for format strings.
authorBruno Haible <bruno@clisp.org>
Sun, 1 Jun 2025 23:52:49 +0000 (01:52 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 2 Jun 2025 00:22:42 +0000 (02:22 +0200)
* gettext-tools/src/format-javascript.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_javascript): Use it.
* gettext-tools/tests/format-javascript-3: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add it.

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

index 810a98ce2771dca03bf6630dab5961a9238641bc..d237bf61fe8ef849dc0df86227818990c478e097 100644 (file)
@@ -73,6 +73,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 'javascript-format' if they don't occur in a context that requires a format
+     string.  */
+  unsigned int likely_intentional_directives;
   unsigned int numbered_arg_count;
   struct numbered_arg *numbered;
 };
@@ -98,6 +103,7 @@ format_parse (const char *format, bool translated, char *fdi,
   struct spec *result;
 
   spec.directives = 0;
+  spec.likely_intentional_directives = 0;
   spec.numbered_arg_count = 0;
   spec.numbered = NULL;
   numbered_allocated = 0;
@@ -110,6 +116,7 @@ format_parse (const char *format, bool translated, char *fdi,
         /* A directive.  */
         unsigned int number = 0;
         enum format_arg_type type;
+        bool likely_intentional = true;
 
         FDI_SET (format - 1, FMTDIR_START);
         spec.directives++;
@@ -142,7 +149,11 @@ format_parse (const char *format, bool translated, char *fdi,
         /* Parse flags.  */
         while (*format == '-' || *format == '+' || *format == ' '
                || *format == '0' || *format == 'I')
-          format++;
+          {
+            if (*format == ' ')
+              likely_intentional = false;
+            format++;
+          }
 
         /* Parse width.  */
         while (c_isdigit (*format))
@@ -237,6 +248,8 @@ format_parse (const char *format, bool translated, char *fdi,
               }
           }
 
+        if (likely_intentional)
+          spec.likely_intentional_directives++;
         FDI_SET (format, FMTDIR_END);
 
         format++;
@@ -320,6 +333,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,
@@ -406,7 +427,7 @@ struct formatstring_parser formatstring_javascript =
   format_parse,
   format_free,
   format_get_number_of_directives,
-  NULL,
+  format_is_unlikely_intentional,
   format_check
 };
 
index 8d70da07114e30cefe32308890056b4d8218071c..1f5a1ff0562470af746bb2083ff4040980c74d6e 100644 (file)
@@ -207,7 +207,7 @@ TESTS = gettext-1 gettext-2 \
        format-go-1 format-go-2 format-go-3 \
        format-java-1 format-java-2 \
        format-java-printf-1 format-java-printf-2 \
-       format-javascript-1 format-javascript-2 \
+       format-javascript-1 format-javascript-2 format-javascript-3 \
        format-kde-1 format-kde-2 \
        format-kde-kuit-1 format-kde-kuit-2 \
        format-librep-1 format-librep-2 \
diff --git a/gettext-tools/tests/format-javascript-3 b/gettext-tools/tests/format-javascript-3
new file mode 100755 (executable)
index 0000000..1a03af8
--- /dev/null
@@ -0,0 +1,53 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test heuristic recognition of JavaScript format strings like "100% complete".
+
+cat <<\EOF > f-js-3.js
+gettext("Test 1a is 100% complete");
+// gettext("Test 1b is 100% complete").format(120); // TODO
+sformat(gettext("Test 1b is 100% complete"), 120);
+
+gettext("Test 2a from 0% complete to 100% complete");
+// gettext("Test 2b from 0% complete to 100% complete").format(120, 121); // TODO
+sformat(gettext("Test 2b from 0% complete to 100% complete"), 120, 121);
+
+gettext("Test 3a of %s is 100% complete");
+// gettext("Test 3b of %s is 100% complete").format("foo", 120); // TODO
+sformat(gettext("Test 3b of %s is 100% complete"), "foo", 120);
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --flag=sformat:1:javascript-format \
+            --omit-header --no-location -d f-js-3.tmp f-js-3.js || Exit 1
+LC_ALL=C tr -d '\r' < f-js-3.tmp.po > f-js-3.po || Exit 1
+
+cat <<\EOF > f-js-3.ok
+msgid "Test 1a is 100% complete"
+msgstr ""
+
+#, javascript-format
+msgid "Test 1b is 100% complete"
+msgstr ""
+
+msgid "Test 2a from 0% complete to 100% complete"
+msgstr ""
+
+#, javascript-format
+msgid "Test 2b from 0% complete to 100% complete"
+msgstr ""
+
+#, javascript-format
+msgid "Test 3a of %s is 100% complete"
+msgstr ""
+
+#, javascript-format
+msgid "Test 3b of %s is 100% complete"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} f-js-3.ok f-js-3.po
+result=$?
+
+exit $result