Improve the recently-added support for properly pluralized error messages
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 4 Jun 2009 18:33:08 +0000 (18:33 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 4 Jun 2009 18:33:08 +0000 (18:33 +0000)
by extending the ereport() API to cater for pluralization directly.  This
is better than the original method of calling ngettext outside the elog.c
code because (1) it avoids double translation, which wastes cycles and in
the worst case could give a wrong result; and (2) it avoids having to use
a different coding method in PL code than in the core backend.  The
client-side uses of ngettext are not touched since neither of these concerns
is very pressing in the client environment.  Per my proposal of yesterday.

17 files changed:
doc/src/sgml/nls.sgml
doc/src/sgml/sources.sgml
src/backend/catalog/dependency.c
src/backend/catalog/pg_proc.c
src/backend/catalog/pg_shdepend.c
src/backend/executor/execQual.c
src/backend/nls.mk
src/backend/parser/parse_func.c
src/backend/postmaster/bgwriter.c
src/backend/utils/error/elog.c
src/include/utils/elog.h
src/pl/plperl/nls.mk
src/pl/plpgsql/src/nls.mk
src/pl/plpgsql/src/pl_exec.c
src/pl/plpython/nls.mk
src/pl/plpython/plpython.c
src/pl/tcl/nls.mk

index 52da8ead956b0b165517ce98f89ff90c0f6d9095..82b78e00fd801113c0fc719a8842d8bbcadbc0a7 100644 (file)
@@ -46,7 +46,7 @@
     <filename>msgmerge</filename>, respectively, in a GNU-compatible
     implementation.  Later, we will try to arrange it so that if you
     use a packaged source distribution, you won't need
-    <filename>xgettext</filename>.  (From CVS, you will still need
+    <filename>xgettext</filename>.  (If working from CVS, you will still need
     it.)  <application>GNU Gettext 0.10.36</application> or later is currently recommended.
    </para>
 
@@ -152,7 +152,7 @@ msgstr "another translated"
     If there are already some <filename>.po</filename> files, then
     someone has already done some translation work.  The files are
     named <filename><replaceable>language</replaceable>.po</filename>,
-    where <replaceable>language</replaceable> is the 
+    where <replaceable>language</replaceable> is the
     <ulink url="http://lcweb.loc.gov/standards/iso639-2/englangn.html">
     ISO 639-1 two-letter language code (in lower case)</ulink>, e.g.,
     <filename>fr.po</filename> for French.  If there is really a need
@@ -224,7 +224,7 @@ gmake update-po
     that gives room for other people to pick up your work.  However,
     you are encouraged to give priority to removing fuzzy entries
     after doing a merge.  Remember that fuzzy entries will not be
-    installed; they only serve as reference what might be the right
+    installed; they only serve as reference for what might be the right
     translation.
    </para>
 
@@ -347,8 +347,8 @@ fprintf(stderr, "panic level %d\n", lvl);
 <programlisting>
 fprintf(stderr, gettext("panic level %d\n"), lvl);
 </programlisting>
-     (<symbol>gettext</symbol> is defined as a no-op if no NLS is
-     configured.)
+     (<symbol>gettext</symbol> is defined as a no-op if NLS support is
+     not configured.)
     </para>
 
     <para>
@@ -421,6 +421,9 @@ fprintf(stderr, gettext("panic level %d\n"), lvl);
          them here.  If the translatable string is not the first
          argument, the item needs to be of the form
          <literal>func:2</literal> (for the second argument).
+         If you have a function that supports pluralized messages,
+         the item should look like <literal>func:1,2</literal>
+         (identifying the singular and plural message arguments).
         </para>
        </listitem>
       </varlistentry>
@@ -451,8 +454,8 @@ fprintf(stderr, gettext("panic level %d\n"), lvl);
 printf("Files were %s.\n", flag ? "copied" : "removed");
 </programlisting>
       The word order within the sentence might be different in other
-      languages.  Also, even if you remember to call gettext() on each
-      fragment, the fragments might not translate well separately.  It's
+      languages.  Also, even if you remember to call <function>gettext()</> on
+      each fragment, the fragments might not translate well separately.  It's
       better to duplicate a little code so that each message to be
       translated is a coherent whole.  Only numbers, file names, and
       such-like run-time variables should be inserted at run time into
@@ -475,13 +478,44 @@ else
     printf("copied %d files", n):
 </programlisting>
       then be disappointed.  Some languages have more than two forms,
-      with some peculiar rules.  We might have a solution for this in
-      the future, but for now the matter is best avoided altogether.
-      You could write:
+      with some peculiar rules.  It's often best to design the message
+      to avoid the issue altogether, for instance like this:
 <programlisting>
 printf("number of copied files: %d", n);
 </programlisting>
      </para>
+
+     <para>
+      If you really want to construct a properly pluralized message,
+      there is support for this, but it's a bit awkward.  When generating
+      a primary or detail error message in <function>ereport()</>, you can
+      write something like this:
+<programlisting>
+errmsg_plural("copied %d file",
+              "copied %d files",
+              n,
+              n)
+</programlisting>
+      The first argument is the format string appropriate for English
+      singular form, the second is the format string appropriate for
+      English plural form, and the third is the integer control value
+      that determines which plural form to use.  Subsequent arguments
+      are formatted per the format string as usual.  (Normally, the
+      pluralization control value will also be one of the values to be
+      formatted, so it has to be written twice.)  In English it only
+      matters whether <replaceable>n</> is 1 or not 1, but in other
+      languages there can be many different plural forms.  The translator
+      sees the two English forms as a group and has the opportunity to
+      supply multiple substitute strings, with the appropriate one being
+      selected based on the run-time value of <replaceable>n</>.
+     </para>
+
+     <para>
+      If you need to pluralize a message that isn't going directly to an
+      <function>errmsg</> or <function>errdetail</> report, you have to use
+      the underlying function <function>ngettext</>.  See the gettext
+      documentation.
+     </para>
     </listitem>
 
     <listitem>
index e6348bd58d51a027cbcc21f1bd467d4e5d12ff43..526233ee55db0a415c799a2e62904f8dc9655e33 100644 (file)
@@ -181,6 +181,19 @@ ereport(ERROR,
      not worth expending translation effort on.
     </para>
    </listitem>
+   <listitem>
+    <para>
+     <function>errmsg_plural(const char *fmt_singular, const char *fmt_plural,
+     unsigned long n, ...)</function> is like <function>errmsg</>, but with
+     support for various plural forms of the message.
+     <replaceable>fmt_singular</> is the English singular format,
+     <replaceable>fmt_plural</> is the English plural format,
+     <replaceable>n</> is the integer value that determines which plural
+     form is needed, and the remaining arguments are formatted according
+     to the selected format string.  For more information see
+     <xref linkend="nls-guidelines">.
+    </para>
+   </listitem>
    <listitem>
     <para>
      <function>errdetail(const char *msg, ...)</function> supplies an optional
@@ -201,6 +214,14 @@ ereport(ERROR,
      sent to the client.
     </para>
    </listitem>
+   <listitem>
+    <para>
+     <function>errdetail_plural(const char *fmt_singular, const char *fmt_plural,
+     unsigned long n, ...)</function> is like <function>errdetail</>, but with
+     support for various plural forms of the message.
+     For more information see <xref linkend="nls-guidelines">.
+    </para>
+   </listitem>
    <listitem>
     <para>
      <function>errhint(const char *msg, ...)</function> supplies an optional
@@ -390,14 +411,14 @@ Hint:       the addendum
    <para>
     There are functions in the backend that will double-quote their own output
     at need (for example, <function>format_type_be</>()).  Do not put
-    additional quotes around the output of such functions. 
+    additional quotes around the output of such functions.
    </para>
 
    <para>
     Rationale: Objects can have names that create ambiguity when embedded in a
     message.  Be consistent about denoting where a plugged-in name starts and
     ends.  But don't clutter messages with unnecessary or duplicate quote
-    marks. 
+    marks.
    </para>
 
   </simplesect>
@@ -413,7 +434,7 @@ Hint:       the addendum
    <para>
     Primary error messages: Do not capitalize the first letter.  Do not end a
     message with a period.  Do not even think about ending a message with an
-    exclamation point. 
+    exclamation point.
    </para>
 
    <para>
@@ -430,7 +451,7 @@ Hint:       the addendum
     long enough to be more than one sentence, they should be split into
     primary and detail parts.)  However, detail and hint messages are longer
     and might need to include multiple sentences.  For consistency, they should
-    follow complete-sentence style even when there's only one sentence. 
+    follow complete-sentence style even when there's only one sentence.
    </para>
 
   </simplesect>
@@ -473,7 +494,7 @@ Hint:       the addendum
    <para>
     Use past tense if an attempt to do something failed, but could perhaps
     succeed next time (perhaps after fixing some problem).  Use present tense
-    if the failure is certainly permanent. 
+    if the failure is certainly permanent.
    </para>
 
    <para>
@@ -489,20 +510,20 @@ cannot open file "%s"
     message should give a reason, such as <quote>disk full</quote> or
     <quote>file doesn't exist</quote>.  The past tense is appropriate because
     next time the disk might not be full anymore or the file in question might
-    exist. 
+    exist.
    </para>
 
    <para>
     The second form indicates that the functionality of opening the named file
     does not exist at all in the program, or that it's conceptually
     impossible.  The present tense is appropriate because the condition will
-    persist indefinitely. 
+    persist indefinitely.
    </para>
 
    <para>
     Rationale: Granted, the average user will not be able to draw great
     conclusions merely from the tense of the message, but since the language
-    provides us with a grammar we should use it correctly. 
+    provides us with a grammar we should use it correctly.
    </para>
 
   </simplesect>
@@ -552,7 +573,7 @@ could not open file %s: %m
     to paste this into a single smooth sentence, so some sort of punctuation
     is needed.  Putting the embedded text in parentheses has also been
     suggested, but it's unnatural if the embedded text is likely to be the
-    most important part of the message, as is often the case. 
+    most important part of the message, as is often the case.
    </para>
 
   </simplesect>
@@ -579,7 +600,7 @@ BETTER: could not open file %s (I/O failure)
     Don't include the name of the reporting routine in the error text. We have
     other mechanisms for finding that out when needed, and for most users it's
     not helpful information.  If the error text doesn't make as much sense
-    without the function name, reword it. 
+    without the function name, reword it.
 <programlisting>
 BAD:    pg_atoi: error in "z": cannot parse "z"
 BETTER: invalid input syntax for integer: "z"
@@ -620,7 +641,7 @@ BETTER: could not open file %s: %m
    <para>
     Error messages like <quote>bad result</quote> are really hard to interpret
     intelligently.  It's better to write why the result is <quote>bad</quote>,
-    e.g., <quote>invalid format</quote>. 
+    e.g., <quote>invalid format</quote>.
    </para>
   </formalpara>
 
@@ -638,7 +659,7 @@ BETTER: could not open file %s: %m
     Try to avoid <quote>unknown</quote>.  Consider <quote>error: unknown
     response</quote>.  If you don't know what the response is, how do you know
     it's erroneous? <quote>Unrecognized</quote> is often a better choice.
-    Also, be sure to include the value being complained of. 
+    Also, be sure to include the value being complained of.
 <programlisting>
 BAD:    unknown node type
 BETTER: unrecognized node type: 42
@@ -654,7 +675,7 @@ BETTER: unrecognized node type: 42
     couldn't <quote>find</quote> the resource.  If, on the other hand, the
     expected location of the resource is known but the program cannot access
     it there then say that the resource doesn't <quote>exist</quote>.  Using
-    <quote>find</quote> in this case sounds weak and confuses the issue. 
+    <quote>find</quote> in this case sounds weak and confuses the issue.
    </para>
   </formalpara>
 
index 3c8c9314cbe52a84579fbcbed17d98287ad2b9b2..237e8d0dbf8d5e74dfcfab89cca257fbb0944cdf 100644 (file)
@@ -914,10 +914,10 @@ reportDependentObjects(const ObjectAddresses *targetObjects,
        {
                ereport(msglevel,
                                /* translator: %d always has a value larger than 1 */
-                               (errmsg(ngettext("drop cascades to %d other object",
-                                                                "drop cascades to %d other objects",
-                                                                numReportedClient + numNotReportedClient),
-                                               numReportedClient + numNotReportedClient),
+                               (errmsg_plural("drop cascades to %d other object",
+                                                          "drop cascades to %d other objects",
+                                                          numReportedClient + numNotReportedClient,
+                                                          numReportedClient + numNotReportedClient),
                                 errdetail("%s", clientdetail.data),
                                 errdetail_log("%s", logdetail.data)));
        }
index ada27d4dd008a04b018650f7f92a4681ede55bf5..420256518fb93a3c4feaaefb9da50bfcf5b90034 100644 (file)
@@ -112,10 +112,10 @@ ProcedureCreate(const char *procedureName,
        if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
                ereport(ERROR,
                                (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                                errmsg(ngettext("functions cannot have more than %d argument",
-                                                                "functions cannot have more than %d arguments",
-                                                                FUNC_MAX_ARGS),
-                                               FUNC_MAX_ARGS)));
+                                errmsg_plural("functions cannot have more than %d argument",
+                                                          "functions cannot have more than %d arguments",
+                                                          FUNC_MAX_ARGS,
+                                                          FUNC_MAX_ARGS)));
        /* note: the above is correct, we do NOT count output arguments */
 
        if (allParameterTypes != PointerGetDatum(NULL))
index 366c6309210ae4a3b6bbf78f5289ca4ea224c7e8..0415f75b2a48f4775e9322a86aa665cc0ef42f58 100644 (file)
@@ -1049,7 +1049,10 @@ storeObjectDescription(StringInfo descs, objectType type,
 
                case REMOTE_OBJECT:
                        /* translator: %s will always be "database %s" */
-                       appendStringInfo(descs, ngettext("%d object in %s", "%d objects in %s", count), count, objdesc);
+                       appendStringInfo(descs, ngettext("%d object in %s",
+                                                                                        "%d objects in %s",
+                                                                                        count),
+                                                        count, objdesc);
                        break;
 
                default:
index c3e7e24531d50ed490307d5d6762497b6fd7d24c..870b3213c601913f916f5192734a15dae65ffb25 100644 (file)
@@ -616,10 +616,11 @@ ExecEvalVar(ExprState *exprstate, ExprContext *econtext,
                                ereport(ERROR,
                                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                                 errmsg("table row type and query-specified row type do not match"),
-                                                errdetail(ngettext("Table row contains %d attribute, but query expects %d.",
-                                                                       "Table row contains %d attributes, but query expects %d.",
-                                                                       slot_tupdesc->natts),
-                                                                  slot_tupdesc->natts, var_tupdesc->natts)));
+                                                errdetail_plural("Table row contains %d attribute, but query expects %d.",
+                                                                                 "Table row contains %d attributes, but query expects %d.",
+                                                                                 slot_tupdesc->natts,
+                                                                                 slot_tupdesc->natts,
+                                                                                 var_tupdesc->natts)));
                        else if (var_tupdesc->natts < slot_tupdesc->natts)
                                needslow = true;
 
@@ -1043,10 +1044,10 @@ init_fcache(Oid foid, FuncExprState *fcache,
        if (list_length(fcache->args) > FUNC_MAX_ARGS)
                ereport(ERROR,
                                (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                                errmsg(ngettext("cannot pass more than %d argument to a function",
-                                                                "cannot pass more than %d arguments to a function",
-                                                                FUNC_MAX_ARGS),
-                                               FUNC_MAX_ARGS)));
+                                errmsg_plural("cannot pass more than %d argument to a function",
+                                                          "cannot pass more than %d arguments to a function",
+                                                          FUNC_MAX_ARGS,
+                                                          FUNC_MAX_ARGS)));
 
        /* Set up the primary fmgr lookup information */
        fmgr_info_cxt(foid, &(fcache->func), fcacheCxt);
@@ -1314,10 +1315,10 @@ tupledesc_match(TupleDesc dst_tupdesc, TupleDesc src_tupdesc)
                ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("function return row and query-specified return row do not match"),
-                                errdetail(ngettext("Returned row contains %d attribute, but query expects %d.",
-                                                       "Returned row contains %d attributes, but query expects %d.",
-                                                       src_tupdesc->natts),
-                                                  src_tupdesc->natts, dst_tupdesc->natts)));
+                                errdetail_plural("Returned row contains %d attribute, but query expects %d.",
+                                                                 "Returned row contains %d attributes, but query expects %d.",
+                                                                 src_tupdesc->natts,
+                                                                 src_tupdesc->natts, dst_tupdesc->natts)));
 
        for (i = 0; i < dst_tupdesc->natts; i++)
        {
index d81a7dee7815884748a891f46ffd44ddc941bcf1..661e48d9a6a53404dada330c4f5a0289b3c68c0b 100644 (file)
@@ -2,7 +2,7 @@
 CATALOG_NAME   := postgres
 AVAIL_LANGUAGES        := af cs de es fr hr hu it ja ko nb nl pt_BR ro ru sk sl sv tr zh_CN zh_TW pl
 GETTEXT_FILES  := + gettext-files
-GETTEXT_TRIGGERS:= _ errmsg errdetail errdetail_log errhint errcontext write_stderr yyerror
+GETTEXT_TRIGGERS:= _ errmsg errmsg_plural:1,2 errdetail errdetail_log errdetail_plural:1,2 errhint errcontext write_stderr yyerror
 
 gettext-files: distprep
        find $(srcdir)/ $(srcdir)/../port/ -name '*.c' -print >$@
index 549b527302322255516923d3b27409b3bbb48fe0..10114bacc672fa1dda7d793a407a323a1f1736e9 100644 (file)
@@ -85,10 +85,10 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
        if (list_length(fargs) > FUNC_MAX_ARGS)
                ereport(ERROR,
                                (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                                errmsg(ngettext("cannot pass more than %d argument to a function",
-                                                                "cannot pass more than %d arguments to a function",
-                                                                FUNC_MAX_ARGS),
-                                               FUNC_MAX_ARGS),
+                                errmsg_plural("cannot pass more than %d argument to a function",
+                                                          "cannot pass more than %d arguments to a function",
+                                                          FUNC_MAX_ARGS,
+                                                          FUNC_MAX_ARGS),
                                 parser_errposition(pstate, location)));
 
        /*
@@ -257,10 +257,10 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
                if (nargsplusdefs >= FUNC_MAX_ARGS)
                        ereport(ERROR,
                                        (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                                        errmsg(ngettext("cannot pass more than %d argument to a function",
-                                                                        "cannot pass more than %d arguments to a function",
-                                                                        FUNC_MAX_ARGS),
-                                                       FUNC_MAX_ARGS),
+                                        errmsg_plural("cannot pass more than %d argument to a function",
+                                                                  "cannot pass more than %d arguments to a function",
+                                                                  FUNC_MAX_ARGS,
+                                                                  FUNC_MAX_ARGS),
                                         parser_errposition(pstate, location)));
 
                actual_arg_types[nargsplusdefs++] = exprType(expr);
@@ -538,10 +538,10 @@ func_select_candidate(int nargs,
        if (nargs > FUNC_MAX_ARGS)
                ereport(ERROR,
                                (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                                errmsg(ngettext("cannot pass more than %d argument to a function",
-                                                                "cannot pass more than %d arguments to a function",
-                                                                FUNC_MAX_ARGS),
-                                               FUNC_MAX_ARGS)));
+                                errmsg_plural("cannot pass more than %d argument to a function",
+                                                          "cannot pass more than %d arguments to a function",
+                                                          FUNC_MAX_ARGS,
+                                                          FUNC_MAX_ARGS)));
 
        /*
         * If any input types are domains, reduce them to their base types. This
@@ -1332,10 +1332,10 @@ LookupFuncNameTypeNames(List *funcname, List *argtypes, bool noError)
        if (argcount > FUNC_MAX_ARGS)
                ereport(ERROR,
                                (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                                errmsg(ngettext("functions cannot have more than %d argument",
-                                                                "functions cannot have more than %d arguments",
-                                                                FUNC_MAX_ARGS),
-                                               FUNC_MAX_ARGS)));
+                                errmsg_plural("functions cannot have more than %d argument",
+                                                          "functions cannot have more than %d arguments",
+                                                          FUNC_MAX_ARGS,
+                                                          FUNC_MAX_ARGS)));
 
        args_item = list_head(argtypes);
        for (i = 0; i < argcount; i++)
@@ -1372,10 +1372,10 @@ LookupAggNameTypeNames(List *aggname, List *argtypes, bool noError)
        if (argcount > FUNC_MAX_ARGS)
                ereport(ERROR,
                                (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                                errmsg(ngettext("functions cannot have more than %d argument",
-                                                                "functions cannot have more than %d arguments",
-                                                                FUNC_MAX_ARGS),
-                                               FUNC_MAX_ARGS)));
+                                errmsg_plural("functions cannot have more than %d argument",
+                                                          "functions cannot have more than %d arguments",
+                                                          FUNC_MAX_ARGS,
+                                                          FUNC_MAX_ARGS)));
 
        i = 0;
        foreach(lc, argtypes)
index 11891778007cd9bedf2e580b6b2c7502390822be..560bca8de2cf292b8d104bbbaaba6f23d55f17a6 100644 (file)
@@ -459,10 +459,10 @@ BackgroundWriterMain(void)
                                (flags & CHECKPOINT_CAUSE_XLOG) &&
                                elapsed_secs < CheckPointWarning)
                                ereport(LOG,
-                                               (errmsg(ngettext("checkpoints are occurring too frequently (%d second apart)",
-                                                                                "checkpoints are occurring too frequently (%d seconds apart)",
-                                                                                elapsed_secs),
-                                                               elapsed_secs),
+                                               (errmsg_plural("checkpoints are occurring too frequently (%d second apart)",
+                                                                          "checkpoints are occurring too frequently (%d seconds apart)",
+                                                                          elapsed_secs,
+                                                                          elapsed_secs),
                                                 errhint("Consider increasing the configuration parameter \"checkpoint_segments\".")));
 
                        /*
index 41b204352da147368f8c5cb4410ceb5ab51f6a7f..d2c1be458e3f1519704b0db0c2ffbc801444e7e2 100644 (file)
@@ -681,6 +681,47 @@ errcode_for_socket_access(void)
                pfree(buf.data); \
        }
 
+/*
+ * Same as above, except for pluralized error messages.  The calling routine
+ * must be declared like "const char *fmt_singular, const char *fmt_plural,
+ * unsigned long n, ...".  Translation is assumed always wanted.
+ */
+#define EVALUATE_MESSAGE_PLURAL(targetfield, appendval)  \
+       { \
+               const char         *fmt; \
+               char               *fmtbuf; \
+               StringInfoData  buf; \
+               /* Internationalize the error format string */ \
+               if (!in_error_recursion_trouble()) \
+                       fmt = dngettext(edata->domain, fmt_singular, fmt_plural, n); \
+               else \
+                       fmt = (n == 1 ? fmt_singular : fmt_plural); \
+               /* Expand %m in format string */ \
+               fmtbuf = expand_fmt_string(fmt, edata); \
+               initStringInfo(&buf); \
+               if ((appendval) && edata->targetfield) \
+                       appendStringInfo(&buf, "%s\n", edata->targetfield); \
+               /* Generate actual output --- have to use appendStringInfoVA */ \
+               for (;;) \
+               { \
+                       va_list         args; \
+                       bool            success; \
+                       va_start(args, n); \
+                       success = appendStringInfoVA(&buf, fmtbuf, args); \
+                       va_end(args); \
+                       if (success) \
+                               break; \
+                       enlargeStringInfo(&buf, buf.maxlen); \
+               } \
+               /* Done with expanded fmt */ \
+               pfree(fmtbuf); \
+               /* Save the completed message into the stack item */ \
+               if (edata->targetfield) \
+                       pfree(edata->targetfield); \
+               edata->targetfield = pstrdup(buf.data); \
+               pfree(buf.data); \
+       }
+
 
 /*
  * errmsg --- add a primary error message text to the current error
@@ -738,6 +779,29 @@ errmsg_internal(const char *fmt,...)
 }
 
 
+/*
+ * errmsg_plural --- add a primary error message text to the current error,
+ * with support for pluralization of the message text
+ */
+int
+errmsg_plural(const char *fmt_singular, const char *fmt_plural,
+                         unsigned long n, ...)
+{
+       ErrorData  *edata = &errordata[errordata_stack_depth];
+       MemoryContext oldcontext;
+
+       recursion_depth++;
+       CHECK_STACK_DEPTH();
+       oldcontext = MemoryContextSwitchTo(ErrorContext);
+
+       EVALUATE_MESSAGE_PLURAL(message, false);
+
+       MemoryContextSwitchTo(oldcontext);
+       recursion_depth--;
+       return 0;                                       /* return value does not matter */
+}
+
+
 /*
  * errdetail --- add a detail error message text to the current error
  */
@@ -780,6 +844,29 @@ errdetail_log(const char *fmt,...)
 }
 
 
+/*
+ * errdetail_plural --- add a detail error message text to the current error,
+ * with support for pluralization of the message text
+ */
+int
+errdetail_plural(const char *fmt_singular, const char *fmt_plural,
+                                unsigned long n, ...)
+{
+       ErrorData  *edata = &errordata[errordata_stack_depth];
+       MemoryContext oldcontext;
+
+       recursion_depth++;
+       CHECK_STACK_DEPTH();
+       oldcontext = MemoryContextSwitchTo(ErrorContext);
+
+       EVALUATE_MESSAGE_PLURAL(detail, false);
+
+       MemoryContextSwitchTo(oldcontext);
+       recursion_depth--;
+       return 0;                                       /* return value does not matter */
+}
+
+
 /*
  * errhint --- add a hint error message text to the current error
  */
index 5b49d7ee340c4b88c155217f4cf80af658ee6bb8..aeaca1bed41d3281d9ee3bdf449e6bb34a6f8319 100644 (file)
        (errstart(elevel, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain) ? \
         (errfinish rest) : (void) 0)
 
-#define ereport(level, rest)   \
-       ereport_domain(level, TEXTDOMAIN, rest)
+#define ereport(elevel, rest)  \
+       ereport_domain(elevel, TEXTDOMAIN, rest)
 
 #define TEXTDOMAIN NULL
 
@@ -132,6 +132,14 @@ errmsg_internal(const char *fmt,...)
    the supplied arguments. */
 __attribute__((format(printf, 1, 2)));
 
+extern int
+errmsg_plural(const char *fmt_singular, const char *fmt_plural,
+                         unsigned long n, ...)
+/* This extension allows gcc to check the format string for consistency with
+   the supplied arguments. */
+__attribute__((format(printf, 1, 4)))
+__attribute__((format(printf, 2, 4)));
+
 extern int
 errdetail(const char *fmt,...)
 /* This extension allows gcc to check the format string for consistency with
@@ -144,6 +152,14 @@ errdetail_log(const char *fmt,...)
    the supplied arguments. */
 __attribute__((format(printf, 1, 2)));
 
+extern int
+errdetail_plural(const char *fmt_singular, const char *fmt_plural,
+                                unsigned long n, ...)
+/* This extension allows gcc to check the format string for consistency with
+   the supplied arguments. */
+__attribute__((format(printf, 1, 4)))
+__attribute__((format(printf, 2, 4)));
+
 extern int
 errhint(const char *fmt,...)
 /* This extension allows gcc to check the format string for consistency with
index b653244acbe0485c033cd6008f03f0f1beaabfea..bcfe408af67f384da6b4a6d1e3dbcd191779f923 100644 (file)
@@ -2,4 +2,4 @@
 CATALOG_NAME   := plperl
 AVAIL_LANGUAGES        := de es fr pt_BR tr
 GETTEXT_FILES  := plperl.c SPI.c
-GETTEXT_TRIGGERS:= errmsg errdetail errdetail_log errhint errcontext
+GETTEXT_TRIGGERS:= errmsg errmsg_plural:1,2 errdetail errdetail_log errdetail_plural:1,2 errhint errcontext
index a1f73ad5d7ae7376af2b483dd08ddf90caef2d41..23f0146ed4d442f84651ccdcb41b93a4474371a5 100644 (file)
@@ -2,7 +2,7 @@
 CATALOG_NAME   := plpgsql
 AVAIL_LANGUAGES        := de es fr ja ro tr
 GETTEXT_FILES  := pl_comp.c pl_exec.c pl_gram.c pl_funcs.c pl_handler.c pl_scan.c
-GETTEXT_TRIGGERS:= _ errmsg errdetail errdetail_log errhint errcontext validate_tupdesc_compat:3 yyerror plpgsql_yyerror
+GETTEXT_TRIGGERS:= _ errmsg errmsg_plural:1,2 errdetail errdetail_log errdetail_plural:1,2 errhint errcontext validate_tupdesc_compat:3 yyerror plpgsql_yyerror
 
 .PHONY: gettext-files
 gettext-files: distprep
index 77d0e8fad4d13034e373b5098abe058b5a43db93..68e88a38601ea0a0240a1690155e587bff8a815f 100644 (file)
@@ -4083,9 +4083,11 @@ exec_eval_expr(PLpgSQL_execstate *estate,
        if (estate->eval_tuptable->tupdesc->natts != 1)
                ereport(ERROR,
                                (errcode(ERRCODE_SYNTAX_ERROR),
-                                errmsg(dngettext(TEXTDOMAIN, "query \"%s\" returned %d column", "query \"%s\" returned %d columns", estate->eval_tuptable->tupdesc->natts),
-                                               expr->query,
-                                               estate->eval_tuptable->tupdesc->natts)));
+                                errmsg_plural("query \"%s\" returned %d column",
+                                                          "query \"%s\" returned %d columns",
+                                                          estate->eval_tuptable->tupdesc->natts,
+                                                          expr->query,
+                                                          estate->eval_tuptable->tupdesc->natts)));
 
        /*
         * Return the result and its type
index 09fc4e8b44132513104e51a8e75c8352904c6694..d5fd3ad8b9cffe53eaf62676f9be1063b2f4994d 100644 (file)
@@ -2,4 +2,4 @@
 CATALOG_NAME   := plpython
 AVAIL_LANGUAGES        := de es fr pt_BR tr
 GETTEXT_FILES  := plpython.c
-GETTEXT_TRIGGERS:= errmsg errdetail errdetail_log errhint errcontext PLy_elog:2 PLy_exception_set:2
+GETTEXT_TRIGGERS:= errmsg errmsg_plural:1,2 errdetail errdetail_log errdetail_plural:1,2 errhint errcontext PLy_elog:2 PLy_exception_set:2 PLy_exception_set_plural:2,3
index e29a02eb748f464ca317214a41a44809abfd0e44..de0817cba97bf709def604155be48b2ace807d27 100644 (file)
@@ -205,9 +205,13 @@ static void PLy_init_interp(void);
 static void PLy_init_plpy(void);
 
 /* call PyErr_SetString with a vprint interface and translation support */
-static void
-PLy_exception_set(PyObject *, const char *,...)
+static void PLy_exception_set(PyObject *, const char *,...)
 __attribute__((format(printf, 2, 3)));
+/* same, with pluralized message */
+static void PLy_exception_set_plural(PyObject *, const char *, const char *,
+                                                                        unsigned long n,...)
+__attribute__((format(printf, 2, 5)))
+__attribute__((format(printf, 3, 5)));
 
 /* Get the innermost python procedure called from the backend */
 static char *PLy_procedure_name(PLyProcedure *);
@@ -2525,9 +2529,11 @@ PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
                        PLy_elog(ERROR, "PL/Python function \"%s\" could not execute plan",
                                         PLy_procedure_name(PLy_curr_procedure));
                sv = PyString_AsString(so);
-               PLy_exception_set(PLy_exc_spi_error,
-                                                 dngettext(TEXTDOMAIN, "Expected sequence of %d argument, got %d: %s", "Expected sequence of %d arguments, got %d: %s", plan->nargs),
-                                                 plan->nargs, nargs, sv);
+               PLy_exception_set_plural(PLy_exc_spi_error,
+                                                                "Expected sequence of %d argument, got %d: %s",
+                                                                "Expected sequence of %d arguments, got %d: %s",
+                                                                plan->nargs,
+                                                                plan->nargs, nargs, sv);
                Py_DECREF(so);
 
                return NULL;
@@ -2941,8 +2947,8 @@ PLy_procedure_name(PLyProcedure * proc)
        return proc->proname;
 }
 
-/* output a python traceback/exception via the postgresql elog
- * function.  not pretty.
+/*
+ * Call PyErr_SetString with a vprint interface and translation support
  */
 static void
 PLy_exception_set(PyObject * exc, const char *fmt,...)
@@ -2957,6 +2963,26 @@ PLy_exception_set(PyObject * exc, const char *fmt,...)
        PyErr_SetString(exc, buf);
 }
 
+/*
+ * The same, pluralized.
+ */
+static void
+PLy_exception_set_plural(PyObject *exc,
+                                                const char *fmt_singular, const char *fmt_plural,
+                                                unsigned long n,...)
+{
+       char            buf[1024];
+       va_list         ap;
+
+       va_start(ap, n);
+       vsnprintf(buf, sizeof(buf),
+                         dngettext(TEXTDOMAIN, fmt_singular, fmt_plural, n),
+                         ap);
+       va_end(ap);
+
+       PyErr_SetString(exc, buf);
+}
+
 /* Emit a PG error or notice, together with any available info about the
  * current Python error.  This should be used to propagate Python errors
  * into PG.
index 0ad96f1539d4070001ea6c51efbf3cf9fbbadcbf..88262d2bc5f90b572e842ea91e83bb6c3b20cba2 100644 (file)
@@ -2,4 +2,4 @@
 CATALOG_NAME   := pltcl
 AVAIL_LANGUAGES        := de es fr pt_BR tr
 GETTEXT_FILES  := pltcl.c
-GETTEXT_TRIGGERS:= errmsg errdetail errdetail_log errhint errcontext
+GETTEXT_TRIGGERS:= errmsg errmsg_plural:1,2 errdetail errdetail_log errdetail_plural:1,2 errhint errcontext