From: Bruno Haible Date: Tue, 5 Feb 2002 13:01:04 +0000 (+0000) Subject: Improved handling of plural forms in header entry. X-Git-Tag: v0.11.1~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9330dd140aba25cbff40f17bb447c2283a0e754a;p=thirdparty%2Fgettext.git Improved handling of plural forms in header entry. --- diff --git a/src/ChangeLog b/src/ChangeLog index 0253fd333..1a2007fb5 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2002-02-03 Bruno Haible + + * xgettext.c (finalize_header): New function. + (main): Call it. + 2002-02-03 Bruno Haible * msginit.c (fill_header): Also replace "PACKAGE" in the comment. diff --git a/src/xgettext.c b/src/xgettext.c index 7d766bdff..a973f5607 100644 --- a/src/xgettext.c +++ b/src/xgettext.c @@ -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 , 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))