From: Bruno Haible Date: Wed, 28 Dec 2005 14:07:19 +0000 (+0000) Subject: Tell Python developers to use named arguments in format strings. X-Git-Tag: v0.15~329 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13cd3d32e599995f84c28cbbd3ce574af7ecab63;p=thirdparty%2Fgettext.git Tell Python developers to use named arguments in format strings. --- diff --git a/gettext-tools/src/ChangeLog b/gettext-tools/src/ChangeLog index 6d429ab51..b0f936db1 100644 --- a/gettext-tools/src/ChangeLog +++ b/gettext-tools/src/ChangeLog @@ -1,3 +1,12 @@ +2005-12-25 Bruno Haible + + Tell Python developers to use named arguments in format strings. + * format.h (get_python_format_unnamed_arg_count): New declaration. + * format-python.c (get_python_format_unnamed_arg_count): New function. + * xgettext.c (warn_format_string): New function. + (remember_a_message, remember_a_message_plural): Call it. + Suggested by Martin von Löwis. + 2005-12-07 Bruno Haible * msgl-iconv.c (iconv_message_list): Fix syntax error. diff --git a/gettext-tools/src/format-python.c b/gettext-tools/src/format-python.c index cc82d3450..610cbf04c 100644 --- a/gettext-tools/src/format-python.c +++ b/gettext-tools/src/format-python.c @@ -511,6 +511,29 @@ struct formatstring_parser formatstring_python = }; +unsigned int +get_python_format_unnamed_arg_count (const char *string) +{ + /* Parse the format string. */ + char *invalid_reason = NULL; + struct spec *descr = + (struct spec *) format_parse (string, false, &invalid_reason); + + if (descr != NULL) + { + unsigned int result = descr->unnamed_arg_count; + + format_free (descr); + return result; + } + else + { + free (invalid_reason); + return 0; + } +} + + #ifdef TEST /* Test program: Print the argument list specification returned by diff --git a/gettext-tools/src/format.h b/gettext-tools/src/format.h index 8d6edbd69..a62a5e051 100644 --- a/gettext-tools/src/format.h +++ b/gettext-tools/src/format.h @@ -106,6 +106,10 @@ extern void get_sysdep_c_format_directives (const char *string, bool translated, struct interval **intervalsp, size_t *lengthp); +/* Returns the number of unnamed arguments consumed by a Python format + string. */ +extern unsigned int get_python_format_unnamed_arg_count (const char *string); + /* Check whether both formats strings contain compatible format specifications. PLURAL_DISTRIBUTION is either NULL or an array of nplurals elements, diff --git a/gettext-tools/src/xgettext.c b/gettext-tools/src/xgettext.c index 36b4bf5f3..354b0bc72 100644 --- a/gettext-tools/src/xgettext.c +++ b/gettext-tools/src/xgettext.c @@ -1842,6 +1842,33 @@ set_format_flags_from_context (enum is_format is_format[NFORMATS], } +static void +warn_format_string (enum is_format is_format[NFORMATS], const char *string, + lex_pos_ty *pos, const char *pretty_msgstr) +{ + if (possible_format_p (is_format[format_python]) + && get_python_format_unnamed_arg_count (string) > 1) + { + char buffer[21]; + + error_with_progname = false; + if (pos->line_number == (size_t)(-1)) + buffer[0] = '\0'; + else + sprintf (buffer, ":%ld", (long) pos->line_number); + multiline_warning (xasprintf (_("%s%s: warning: "), + pos->file_name, buffer), + xasprintf (_("\ +'%s' format string with unnamed arguments cannot be properly localized:\n\ +The translator cannot reorder the arguments.\n\ +Please consider using a format string with named arguments,\n\ +and a mapping instead of a tuple for the arguments.\n"), + pretty_msgstr)); + error_with_progname = true; + } +} + + message_ty * remember_a_message (message_list_ty *mlp, char *msgctxt, char *msgid, flag_context_ty context, lex_pos_ty *pos, @@ -2069,6 +2096,10 @@ meta information, not the empty string.\n"))); mp->do_wrap = do_wrap == no ? no : yes; /* By default we wrap. */ + /* Warn about the use of non-reorderable format strings when the programming + language also provides reorderable format strings. */ + warn_format_string (is_format, mp->msgid, pos, "msgid"); + /* Remember where we saw this msgid. */ if (line_comment) message_comment_filepos (mp, pos->file_name, pos->line_number); @@ -2162,6 +2193,10 @@ remember_a_message_plural (message_ty *mp, char *string, free (invalid_reason); } } + + /* Warn about the use of non-reorderable format strings when the programming + language also provides reorderable format strings. */ + warn_format_string (mp->is_format, mp->msgid_plural, pos, "msgid_plural"); } else free (msgid_plural);