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?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
User
  • 62,498
  • 72
  • 186
  • 247

1 Answers1

97

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

dharol
  • 151
  • 2
  • 17
Steve Jalim
  • 11,989
  • 1
  • 37
  • 54