[Python-Dev] PEP 292, Simpler String Substitutions
Fredrik Lundh
fredrik@pythonware.com
Wed, 19 Jun 2002 15:13:25 +0200
guido wrote:
> > > def birth(self, name):
> > > country =3D self.countryOfOrigin['name']
> > > return '${name} was born in ${country}'.sub()
> >=20
> > now explain why the above is a vast improvement over:
> >=20
> > def birth(self, name):
> > country =3D self.countryOfOrigin['name']
> > return join(name, ' was born in ', country)
>=20
> One word: I18n.
really? who's doing the localization in:
return '${name} was born in ${country}'.sub()
maybe barry meant
return _('${name} was born in ${country}').sub()
but in that case, I completely fail to see why he couldn't
just as well do the substitution inside the "_" function:
return _('${name} was born in ${country}')
where _ could be defined as:
def _(string, mapping=3DNone):
if mapping is None:
...
def repl(m, mapping=3Dmapping):
return mapping[m.group(m.lastindex)]
return sre.sub(A_PATTERN, repl, do_translation(string))
instead of
def _(string, mapping=3DNone):
if mapping is None:
...
return do_translation(string).sub(mapping)
</F>