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

Question:

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?

Asked By: User

||

Answers:

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

Answered By: Steve Jalim