]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Handle comments before duplicated msgids in a better way.
authorBruno Haible <bruno@clisp.org>
Fri, 7 Nov 2003 14:04:57 +0000 (14:04 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:11:14 +0000 (12:11 +0200)
gettext-tools/src/ChangeLog
gettext-tools/src/xgettext.c
gettext-tools/tests/ChangeLog
gettext-tools/tests/Makefile.am
gettext-tools/tests/xgettext-7 [new file with mode: 0755]

index f6b3b343553bf100e63a1baaea19be8a405a89de..85bafb4f06a12101fa18642f14efdd3043a3718e 100644 (file)
@@ -1,3 +1,9 @@
+2003-11-07  Bruno Haible  <bruno@clisp.org>
+
+       * xgettext.c (remember_a_message): Omit the programmer comments of a
+       duplicated msgid only if they are redundant.
+       Reported by Christian Neumair <chris@gnome-de.org>.
+
 2003-11-05  Bruno Haible  <bruno@clisp.org>
 
        * user-email.sh.in: Renamed from user-email.in. Internationalize, use
index 0e1eabb50560df84cd6c77032467a59b96766f87..328a15752ef93d79ab3f5e95f0515d7c05a42699 100644 (file)
@@ -1748,66 +1748,97 @@ meta information, not the empty string.\n")));
      string.  */
   set_format_flags_from_context (is_format, context, mp->msgid, pos, "msgid");
 
-  /* Ask the lexer for the comments it has seen.  Only do this for the
-     first instance, otherwise there could be problems; especially if
-     the same comment appears before each.  */
-  if (!mp->comment_dot)
-    {
-      int j;
-      bool add_all_remaining_comments;
+  /* Ask the lexer for the comments it has seen.  */
+  {
+    size_t nitems_before;
+    size_t nitems_after;
+    int j;
+    bool add_all_remaining_comments;
 
-      add_all_remaining_comments = add_all_comments;
-      for (j = 0; ; ++j)
-       {
-         const char *s = xgettext_comment (j);
-         const char *t;
-         if (s == NULL)
-           break;
+    nitems_before = (mp->comment_dot != NULL ? mp->comment_dot->nitems : 0);
 
-         CONVERT_STRING (s);
+    add_all_remaining_comments = add_all_comments;
+    for (j = 0; ; ++j)
+      {
+       const char *s = xgettext_comment (j);
+       const char *t;
+       if (s == NULL)
+         break;
 
-         /* To reduce the possibility of unwanted matches we do a two
-            step match: the line must contain `xgettext:' and one of
-            the possible format description strings.  */
-         if ((t = strstr (s, "xgettext:")) != NULL)
-           {
-             bool tmp_fuzzy;
-             enum is_format tmp_format[NFORMATS];
-             enum is_wrap tmp_wrap;
-             bool interesting;
+       CONVERT_STRING (s);
 
-             t += strlen ("xgettext:");
+       /* To reduce the possibility of unwanted matches we do a two
+          step match: the line must contain `xgettext:' and one of
+          the possible format description strings.  */
+       if ((t = strstr (s, "xgettext:")) != NULL)
+         {
+           bool tmp_fuzzy;
+           enum is_format tmp_format[NFORMATS];
+           enum is_wrap tmp_wrap;
+           bool interesting;
 
-             po_parse_comment_special (t, &tmp_fuzzy, tmp_format, &tmp_wrap);
+           t += strlen ("xgettext:");
 
-             interesting = false;
-             for (i = 0; i < NFORMATS; i++)
-               if (tmp_format[i] != undecided)
-                 {
-                   is_format[i] = tmp_format[i];
-                   interesting = true;
-                 }
-             if (tmp_wrap != undecided)
+           po_parse_comment_special (t, &tmp_fuzzy, tmp_format, &tmp_wrap);
+
+           interesting = false;
+           for (i = 0; i < NFORMATS; i++)
+             if (tmp_format[i] != undecided)
                {
-                 do_wrap = tmp_wrap;
+                 is_format[i] = tmp_format[i];
                  interesting = true;
                }
+           if (tmp_wrap != undecided)
+             {
+               do_wrap = tmp_wrap;
+               interesting = true;
+             }
 
-             /* If the "xgettext:" marker was followed by an interesting
-                keyword, and we updated our is_format/do_wrap variables,
-                we don't print the comment as a #. comment.  */
-             if (interesting)
-               continue;
-           }
-         /* When the comment tag is seen, it drags in not only the line
-            which it starts, but all remaining comment lines.  */
-         if (add_all_remaining_comments
-             || (add_all_remaining_comments =
-                   (comment_tag != NULL
-                    && strncmp (s, comment_tag, strlen (comment_tag)) == 0)))
-           message_comment_dot_append (mp, s);
-       }
-    }
+           /* If the "xgettext:" marker was followed by an interesting
+              keyword, and we updated our is_format/do_wrap variables,
+              we don't print the comment as a #. comment.  */
+           if (interesting)
+             continue;
+         }
+       /* When the comment tag is seen, it drags in not only the line
+          which it starts, but all remaining comment lines.  */
+       if (add_all_remaining_comments
+           || (add_all_remaining_comments =
+                 (comment_tag != NULL
+                  && strncmp (s, comment_tag, strlen (comment_tag)) == 0)))
+         message_comment_dot_append (mp, s);
+      }
+
+    nitems_after = (mp->comment_dot != NULL ? mp->comment_dot->nitems : 0);
+
+    /* Don't add the comments if they are a repetition of the tail of the
+       already present comments.  This avoids unneeded duplication if the
+       same message appears several times, each time with the same comment.  */
+    if (nitems_before < nitems_after)
+      {
+       size_t added = nitems_after - nitems_before;
+
+       if (added <= nitems_before)
+         {
+           bool repeated = true;
+
+           for (i = 0; i < added; i++)
+             if (strcmp (mp->comment_dot->item[nitems_before - added + i],
+                         mp->comment_dot->item[nitems_before + i]) != 0)
+               {
+                 repeated = false;
+                 break;
+               }
+
+           if (repeated)
+             {
+               for (i = 0; i < added; i++)
+                 free ((char *) mp->comment_dot->item[nitems_before + i]);
+               mp->comment_dot->nitems = nitems_before;
+             }
+         }
+      }
+  }
 
   /* If it is not already decided, through programmer comments, whether the
      msgid is a format string, examine the msgid.  This is a heuristic.  */
index 140c5d63342bfc7d92f106c1195089320c5750a8..6dcee57da02f59e93b00c36964e472e8afa86f8c 100644 (file)
@@ -1,3 +1,8 @@
+2003-11-07  Bruno Haible  <bruno@clisp.org>
+
+       * xgettext-7: New file.
+       * Makefile.am (TESTS): Add it.
+
 2003-11-06  Bruno Haible  <bruno@clisp.org>
 
        * msgcat-stringtable-1: Fix expected test result.
index 8167eb9353e4a9b73a942e26b325d8b4df4ee473..bf24bd34d608c4da16ca5948e2109d83d4d559c7 100644 (file)
@@ -56,6 +56,7 @@ TESTS = gettext-1 gettext-2 \
        msgunfmt-tcl-1 \
        msguniq-1 msguniq-2 msguniq-3 msguniq-4 \
        xgettext-1 xgettext-2 xgettext-3 xgettext-4 xgettext-5 xgettext-6 \
+       xgettext-7 \
        xgettext-c-1 xgettext-c-2 xgettext-c-3 xgettext-c-4 xgettext-c-5 \
        xgettext-c-6 xgettext-c-7 xgettext-c-8 \
        xgettext-glade-1 xgettext-glade-2 xgettext-glade-3 \
diff --git a/gettext-tools/tests/xgettext-7 b/gettext-tools/tests/xgettext-7
new file mode 100755 (executable)
index 0000000..d78b93b
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+# Test of comment extraction in the case of duplicated msgids.
+
+tmpfiles=""
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+tmpfiles="$tmpfiles xg-test7.c"
+cat <<\EOF > xg-test7.c
+/* first comment */
+/* xgettext: c-format */
+gettext ("abc");
+
+/* first comment */
+/* xgettext: lisp-format */
+gettext ("abc");
+
+/* second comment */
+/* xgettext: python-format */
+gettext ("abc");
+EOF
+
+tmpfiles="$tmpfiles xg-test6.po"
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --add-comments -d xg-test7 xg-test7.c
+test $? = 0 || { rm -fr $tmpfiles; exit 1; }
+
+tmpfiles="$tmpfiles xg-test7.ok"
+cat <<\EOF > xg-test7.ok
+#. first comment
+#. second comment
+#: xg-test7.c:3 xg-test7.c:7 xg-test7.c:11
+#, c-format, python-format, lisp-format
+msgid "abc"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-test7.ok xg-test7.po
+result=$?
+
+rm -fr $tmpfiles
+
+exit $result