Meaning of leading underscore in list of tuples used to define choice fields?
Asked Answered
G

1

67

I've seen a few examples defining choice fields like so:

COUNTRIES = (
    ('fr', _('France')),
    ('de', _('Germany')),
    ...
)

(Source: http://code.djangoproject.com/ticket/5446 Also see: http://djangosnippets.org/snippets/494/)

What is the meaning of the leading underscores? And why is the second value in the tuple even parenthesized?

Geum answered 3/6, 2010 at 8:12 Comment(0)
F
98

The leading underscore is the commonly used function alias for the one of the ugettext functions used by the internationalization (i18n) mechanics.

It means that when you have i18n running, the choicefield labels will be translated into the appropriate end-user language, if a translation is available.

At the top of a file that features this kind of syntax, you should see (or if not, you should have) something like:

from django.utils.translation import ugettext_lazy as _

See the docs here for more details

Filature answered 3/6, 2010 at 8:15 Comment(1)
For Django 4: from django.utils.translation import gettext as _Octahedral

© 2022 - 2024 — McMap. All rights reserved.