]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Improved handling of plural forms in header entry.
authorBruno Haible <bruno@clisp.org>
Tue, 5 Feb 2002 13:01:04 +0000 (13:01 +0000)
committerBruno Haible <bruno@clisp.org>
Sun, 21 Jun 2009 23:15:52 +0000 (01:15 +0200)
src/ChangeLog
src/xgettext.c

index 0253fd333e9ac661711ca9742b0e6f837555c1a3..1a2007fb5dffabba9bc9de1c7e4857d07b3536e5 100644 (file)
@@ -1,3 +1,8 @@
+2002-02-03  Bruno Haible  <bruno@clisp.org>
+
+       * xgettext.c (finalize_header): New function.
+       (main): Call it.
+
 2002-02-03  Bruno Haible  <bruno@clisp.org>
 
        * msginit.c (fill_header): Also replace "PACKAGE" in the comment.
index 7d766bdff186ceeabb8264003f2cdae959eed1a0..a973f56079fe0645ca9fe4497724ef6cc4be5156 100644 (file)
@@ -174,6 +174,7 @@ static void extract_from_file PARAMS ((const char *file_name,
                                       extractor_func extractor,
                                       msgdomain_list_ty *mdlp));
 static message_ty *construct_header PARAMS ((void));
+static void finalize_header PARAMS ((msgdomain_list_ty *mdlp));
 static extractor_func language_to_extractor PARAMS ((const char *name));
 static const char *extension_to_language PARAMS ((const char *extension));
 
@@ -486,6 +487,10 @@ warning: file `%s' extension `%s' is unknown; will try C"), fname, extension);
     }
   string_list_free (file_list);
 
+  /* Finalize the constructed header.  */
+  if (!xgettext_omit_header)
+    finalize_header (mdlp);
+
   /* Sorting the list of messages.  */
   if (sort_by_filepos)
     msgdomain_list_sort_by_filepos (mdlp);
@@ -1163,6 +1168,61 @@ FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n");
   return mp;
 }
 
+static void
+finalize_header (mdlp)
+     msgdomain_list_ty *mdlp;
+{
+  /* If the generated PO file has plural forms, add a Plural-Forms template
+     to the constructed header.  */
+  bool has_plural;
+  size_t i, j;
+
+  has_plural = false;
+  for (i = 0; i < mdlp->nitems; i++)
+    {
+      message_list_ty *mlp = mdlp->item[i]->messages;
+
+      for (j = 0; j < mlp->nitems; j++)
+       {
+         message_ty *mp = mlp->item[j];
+
+         if (mp->msgid_plural != NULL)
+           {
+             has_plural = true;
+             break;
+           }
+       }
+      if (has_plural)
+       break;
+    }
+
+  if (has_plural)
+    {
+      message_ty *header = message_list_search (mdlp->item[0]->messages, "");
+      if (header != NULL
+         && strstr (header->msgstr, "Plural-Forms:") == NULL)
+       {
+         size_t insertpos = strlen (header->msgstr);
+         const char *suffix;
+         size_t suffix_len;
+         char *new_msgstr;
+
+         suffix = "\nPlural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n";
+         if (insertpos == 0 || header->msgstr[insertpos-1] == '\n')
+           suffix++;
+         suffix_len = strlen (suffix);
+         new_msgstr = (char *) xmalloc (header->msgstr_len + suffix_len);
+         memcpy (new_msgstr, header->msgstr, insertpos);
+         memcpy (new_msgstr + insertpos, suffix, suffix_len);
+         memcpy (new_msgstr + insertpos + suffix_len,
+                 header->msgstr + insertpos,
+                 header->msgstr_len - insertpos);
+         header->msgstr = new_msgstr;
+         header->msgstr_len = header->msgstr_len + suffix_len;
+       }
+    }
+}
+
 
 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
 #define ENDOF(a) ((a) + SIZEOF(a))