Shouldn't _ be ugettext instead of gettext?

639 views
Skip to first unread message

Michael Radziej

unread,
Aug 15, 2007, 10:52:00 AM8/15/07
to django-d...@googlegroups.com
Hi,

currently, django/conf/__init__.py still sets __builtins__['_'] = gettext.

This causes a UnicodeDecodeError in code like the following line when the
the translated message contains non-ASCII characters:

_('some message: %s').someModel.name

Shouldn't _ better evaluate to ugettext?


Michael

--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100
http://www.noris.de - The IT-Outsourcing Company

Vorstand: Ingo Kraupa (Vorsitzender), Joachim Astel, Hansjochen Klenk -
Vorsitzender des Aufsichtsrats: Stefan Schnabel - AG Nürnberg HRB 17689

Malcolm Tredinnick

unread,
Aug 15, 2007, 11:15:13 AM8/15/07
to django-d...@googlegroups.com
Hey Michael,

On Wed, 2007-08-15 at 16:52 +0200, Michael Radziej wrote:
> Hi,
>
> currently, django/conf/__init__.py still sets __builtins__['_'] = gettext.
>
> This causes a UnicodeDecodeError in code like the following line when the
> the translated message contains non-ASCII characters:
>
> _('some message: %s').someModel.name
>
> Shouldn't _ better evaluate to ugettext?

The short answer: You're right that the default isn't so useful. But the
alternative isn't necessarily so useful, either. So #2920 comes into
play.

The longer answer...

It makes sense in the default Python setup to have _() in builtins. It
makes a lot less sense to do so in Django. So, as part of ticket #2920
(and a discussion here a long time ago), we've decided to remove _()
from the default builtins altogether. The logic is that sometimes
ugettext() makes sense, sometimes (for some users) it's gettext(),
sometimes it might be ugettext_lazy() -- a feature that doesn't exist in
Python's standard gettext library, but is obviously useful. there's also
the well-documented problems that aliasing _ interferes with both
doctests and the interactive prompt, where it has another useful
meaning.

So we need to force people to make the right decision, which means they
have to actually make a conscious decision and we shouldn't be
guessing.

I haven't committed that change yet, mostly because it's
backwards-incompatible and I don't want to just dribble such changes
into the tree. So I'm kind of bunching them up. Since autoescape is
going to also be backwards incompatible, I might drop it in around the
same time. I need to work out if there are any other settled, but
pending changes of that nature. Since people need to do some changes to
their code, we should be nice and try to group them together, rather
than having change-of-the-week -- at least that's my personal thinking.

Regards,
malcolm

--
On the other hand, you have different fingers.
http://www.pointy-stick.com/blog/

Christopher Lenz

unread,
Aug 15, 2007, 11:35:23 AM8/15/07
to django-d...@googlegroups.com
Am 15.08.2007 um 16:52 schrieb Michael Radziej:
> currently, django/conf/__init__.py still sets __builtins__['_'] =
> gettext.
>
> This causes a UnicodeDecodeError in code like the following line
> when the
> the translated message contains non-ASCII characters:
>
> _('some message: %s').someModel.name
>
> Shouldn't _ better evaluate to ugettext?

See also <http://code.djangoproject.com/ticket/5056>

(I still don't see why you'd ever want the bytestring variants of
gettext, but don't want to waste any more time arguing about this
stuff).

Cheers,
Chris
--
Christopher Lenz
cmlenz at gmx.de
http://www.cmlenz.net/

Reply all
Reply to author
Forward
0 new messages