File: gettext.info, Node: No custom format directives, Next: No unusual markup, Prev: No embedded URLs, Up: Preparing Strings
4.3.6 No programmer-defined format string directives
----------------------------------------------------
The GNU C Library's ‘’ facility and the C++ standard
library's ‘’ header file make it possible for the programmer to
define their own format string directives. However, such format
directives cannot be used in translatable strings, for two reasons:
• There is no reference documentation for format strings with such
directives, that the translators could consult. They would
therefore have to guess where the directive starts and where it
ends.
• An ‘msgfmt -c’ invocation cannot check whether the translator has
produced a compatible translation of the format string. As a
consequence, when a format string contains a programmer-defined
directive, the program may crash at runtime when it uses the
translated format string.
To avoid this situation, you need to move the formatting with the
custom directive into a format string that does not get translated.
For example, assuming code that makes use of a ‘%r’ directive:
fprintf (stream, _("The contents is: %r"), data);
you would rewrite it to:
char *tmp;
if (asprintf (&tmp, "%r", data) < 0)
error (...);
fprintf (stream, _("The contents is: %s"), tmp);
free (tmp);
Similarly, in C++, assuming you have defined a custom ‘formatter’ for
the type of ‘data’, the code
cout << format (_("The contents is: {:#$#}"), data);
should be rewritten to:
string tmp = format ("{:#$#}", data);
cout << format (_("The contents is: {}"), tmp);