From: Tom Lane Date: Thu, 4 Jun 2009 18:33:08 +0000 (+0000) Subject: Improve the recently-added support for properly pluralized error messages X-Git-Tag: REL8_4_RC1~54 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=76d4abf2d974ffa578ddc7ff40984cc05c1d48b1;p=postgresql Improve the recently-added support for properly pluralized error messages 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. --- diff --git a/doc/src/sgml/nls.sgml b/doc/src/sgml/nls.sgml index 3a567df16c..063b575328 100644 --- a/doc/src/sgml/nls.sgml +++ b/doc/src/sgml/nls.sgml @@ -1,4 +1,4 @@ - + @@ -46,7 +46,7 @@ msgmerge, 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 - xgettext. (From CVS, you will still need + xgettext. (If working from CVS, you will still need it.) GNU Gettext 0.10.36 or later is currently recommended. @@ -152,7 +152,7 @@ msgstr "another translated" If there are already some .po files, then someone has already done some translation work. The files are named language.po, - where language is the + where language is the ISO 639-1 two-letter language code (in lower case), e.g., fr.po 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. @@ -347,8 +347,8 @@ fprintf(stderr, "panic level %d\n", lvl); fprintf(stderr, gettext("panic level %d\n"), lvl); - (gettext is defined as a no-op if no NLS is - configured.) + (gettext is defined as a no-op if NLS support is + not configured.) @@ -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 func:2 (for the second argument). + If you have a function that supports pluralized messages, + the item should look like func:1,2 + (identifying the singular and plural message arguments). @@ -451,8 +454,8 @@ fprintf(stderr, gettext("panic level %d\n"), lvl); printf("Files were %s.\n", flag ? "copied" : "removed"); 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 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): 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: printf("number of copied files: %d", n); + + + 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 ereport(), you can + write something like this: + +errmsg_plural("copied %d file", + "copied %d files", + n, + n) + + 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 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 n. + + + + If you need to pluralize a message that isn't going directly to an + errmsg or errdetail report, you have to use + the underlying function ngettext. See the gettext + documentation. + diff --git a/doc/src/sgml/sources.sgml b/doc/src/sgml/sources.sgml index 426ab01fe1..0872eacee7 100644 --- a/doc/src/sgml/sources.sgml +++ b/doc/src/sgml/sources.sgml @@ -1,4 +1,4 @@ - + PostgreSQL Coding Conventions @@ -181,6 +181,19 @@ ereport(ERROR, not worth expending translation effort on. + + + errmsg_plural(const char *fmt_singular, const char *fmt_plural, + unsigned long n, ...) is like errmsg, but with + support for various plural forms of the message. + fmt_singular is the English singular format, + fmt_plural is the English plural format, + 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 + . + + errdetail(const char *msg, ...) supplies an optional @@ -201,6 +214,14 @@ ereport(ERROR, sent to the client. + + + errdetail_plural(const char *fmt_singular, const char *fmt_plural, + unsigned long n, ...) is like errdetail, but with + support for various plural forms of the message. + For more information see . + + errhint(const char *msg, ...) supplies an optional @@ -390,14 +411,14 @@ Hint: the addendum There are functions in the backend that will double-quote their own output at need (for example, format_type_be()). Do not put - additional quotes around the output of such functions. + additional quotes around the output of such functions. 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. @@ -413,7 +434,7 @@ Hint: the addendum 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. @@ -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. @@ -473,7 +494,7 @@ Hint: the addendum 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. @@ -489,20 +510,20 @@ cannot open file "%s" message should give a reason, such as disk full or file doesn't exist. The past tense is appropriate because next time the disk might not be full anymore or the file in question might - exist. + exist. 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. 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. @@ -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. @@ -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. 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 Error messages like bad result are really hard to interpret intelligently. It's better to write why the result is bad, - e.g., invalid format. + e.g., invalid format. @@ -638,7 +659,7 @@ BETTER: could not open file %s: %m Try to avoid unknown. Consider error: unknown response. If you don't know what the response is, how do you know it's erroneous? Unrecognized 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. BAD: unknown node type BETTER: unrecognized node type: 42 @@ -654,7 +675,7 @@ BETTER: unrecognized node type: 42 couldn't find 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 exist. Using - find in this case sounds weak and confuses the issue. + find in this case sounds weak and confuses the issue. diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c index 957cac49f3..377ae8b712 100644 --- a/src/backend/catalog/dependency.c +++ b/src/backend/catalog/dependency.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/catalog/dependency.c,v 1.87 2009/03/26 22:26:06 petere Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/dependency.c,v 1.88 2009/06/04 18:33:06 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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))); } diff --git a/src/backend/catalog/pg_proc.c b/src/backend/catalog/pg_proc.c index ad9539234f..ce42910e78 100644 --- a/src/backend/catalog/pg_proc.c +++ b/src/backend/catalog/pg_proc.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.162 2009/03/26 22:26:06 petere Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.163 2009/06/04 18:33:06 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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)) diff --git a/src/backend/catalog/pg_shdepend.c b/src/backend/catalog/pg_shdepend.c index a4de276b8d..19cfe08f4f 100644 --- a/src/backend/catalog/pg_shdepend.c +++ b/src/backend/catalog/pg_shdepend.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.32 2009/03/26 22:26:06 petere Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.33 2009/06/04 18:33:06 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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: diff --git a/src/backend/executor/execQual.c b/src/backend/executor/execQual.c index d9cbb1d763..c7bfe7c76c 100644 --- a/src/backend/executor/execQual.c +++ b/src/backend/executor/execQual.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.246 2009/04/08 21:51:38 petere Exp $ + * $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.247 2009/06/04 18:33:07 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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++) { diff --git a/src/backend/nls.mk b/src/backend/nls.mk index 8e66d2d199..3a79ca5048 100644 --- a/src/backend/nls.mk +++ b/src/backend/nls.mk @@ -1,8 +1,8 @@ -# $PostgreSQL: pgsql/src/backend/nls.mk,v 1.25 2009/05/14 21:41:50 alvherre Exp $ +# $PostgreSQL: pgsql/src/backend/nls.mk,v 1.26 2009/06/04 18:33:07 tgl Exp $ 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 >$@ diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index 38008b9cea..260f74d595 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_func.c,v 1.214 2009/05/12 00:56:05 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_func.c,v 1.215 2009/06/04 18:33:07 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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) diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c index 7a23c0130d..b2a90528b6 100644 --- a/src/backend/postmaster/bgwriter.c +++ b/src/backend/postmaster/bgwriter.c @@ -37,7 +37,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.58 2009/05/15 15:56:39 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.59 2009/06/04 18:33:07 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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\"."))); /* diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 0439c4c1d1..d93aaeb54d 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -42,7 +42,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.213 2009/03/02 21:18:43 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.214 2009/06/04 18:33:07 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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 */ diff --git a/src/include/utils/elog.h b/src/include/utils/elog.h index 6c90696569..81ddb9fc37 100644 --- a/src/include/utils/elog.h +++ b/src/include/utils/elog.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/utils/elog.h,v 1.99 2009/01/06 16:39:52 tgl Exp $ + * $PostgreSQL: pgsql/src/include/utils/elog.h,v 1.100 2009/06/04 18:33:07 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -106,8 +106,8 @@ (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 diff --git a/src/pl/plperl/nls.mk b/src/pl/plperl/nls.mk index 8eaf6dd773..030886c0bb 100644 --- a/src/pl/plperl/nls.mk +++ b/src/pl/plperl/nls.mk @@ -1,5 +1,5 @@ -# $PostgreSQL: pgsql/src/pl/plperl/nls.mk,v 1.5 2009/05/14 21:41:53 alvherre Exp $ +# $PostgreSQL: pgsql/src/pl/plperl/nls.mk,v 1.6 2009/06/04 18:33:07 tgl Exp $ 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 diff --git a/src/pl/plpgsql/src/nls.mk b/src/pl/plpgsql/src/nls.mk index 7f17b88eec..ffebf4b21e 100644 --- a/src/pl/plpgsql/src/nls.mk +++ b/src/pl/plpgsql/src/nls.mk @@ -1,8 +1,8 @@ -# $PostgreSQL: pgsql/src/pl/plpgsql/src/nls.mk,v 1.7 2009/05/14 21:41:53 alvherre Exp $ +# $PostgreSQL: pgsql/src/pl/plpgsql/src/nls.mk,v 1.8 2009/06/04 18:33:07 tgl Exp $ 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 diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index e436e053b1..b5f0006918 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.241 2009/05/02 17:27:57 tgl Exp $ + * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.242 2009/06/04 18:33:07 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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 diff --git a/src/pl/plpython/nls.mk b/src/pl/plpython/nls.mk index 5721dbf445..6e54a35312 100644 --- a/src/pl/plpython/nls.mk +++ b/src/pl/plpython/nls.mk @@ -1,5 +1,5 @@ -# $PostgreSQL: pgsql/src/pl/plpython/nls.mk,v 1.4 2009/05/14 21:41:53 alvherre Exp $ +# $PostgreSQL: pgsql/src/pl/plpython/nls.mk,v 1.5 2009/06/04 18:33:08 tgl Exp $ 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 diff --git a/src/pl/plpython/plpython.c b/src/pl/plpython/plpython.c index 61e4772e8b..345f3f6523 100644 --- a/src/pl/plpython/plpython.c +++ b/src/pl/plpython/plpython.c @@ -1,7 +1,7 @@ /********************************************************************** * plpython.c - python as a procedural language for PostgreSQL * - * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.120 2009/04/03 16:59:42 tgl Exp $ + * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.121 2009/06/04 18:33:08 tgl Exp $ * ********************************************************************* */ @@ -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. diff --git a/src/pl/tcl/nls.mk b/src/pl/tcl/nls.mk index 9188911f76..f331e658a9 100644 --- a/src/pl/tcl/nls.mk +++ b/src/pl/tcl/nls.mk @@ -1,5 +1,5 @@ -# $PostgreSQL: pgsql/src/pl/tcl/nls.mk,v 1.4 2009/05/14 21:41:53 alvherre Exp $ +# $PostgreSQL: pgsql/src/pl/tcl/nls.mk,v 1.5 2009/06/04 18:33:08 tgl Exp $ 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