[Python-Dev] string formatting and i18n

Gareth McCaughan gmccaughan at synaptics-uk.com
Mon Sep 5 18:57:53 CEST 2005


On Monday 2005-09-05 17:07, Antoine Pitrou wrote:

> Le lundi 05 septembre 2005 à 16:52 +0100, Gareth McCaughan a écrit :
> > ... and should add: Of course it's usually seen as being about
> > output more than about formatting, but in fact if you want
> > to do what Python does with "%", C with "sprintf" and
> > Common Lisp with (format nil ...) then the Right Thing in C++
> > (in so far as that exists) is usually to use << with a string
> > stream.
> 
> Uh, what about internationalization (i18n) ?
> In i18n you can't avoid the need for parameterized strings.
> For example I want to write :
> 	_("The file '%s' is read only") % filename
> not :
> 	_("The file") + " '" + filename + "' " + _("is read only")
> 
> because the splitting in the second form will not translate correctly
> into other languages. You *have* to supply the whole non-splitted
> sentence to the translators.

Yes. If you think I was arguing the opposite, then I failed to
communicate clearly and I apologize.

> The bottom line, IMHO, is that there are frequent uses that mandate a
> nice and easy to use formatting operator. Python has it, let's not
> remove it.

It's clear (I think) that a good way of formatting strings is
necessary. It's less clear what the best way is. Python's % is
pretty good; perhaps it's possible to do even better.

For instance, take your I18N example. Not all languages have the
same word order, as you've observed. When there's more than one
parameter, Python's %-interpolation isn't enough in general;
you'd need something that can reorder the parameters. I don't
know whether this is worth complicating string formatting for,
but it's not obvious that it isn't.

-- 
g



More information about the Python-Dev mailing list