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

PythonDjangoInternationalization

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUserView Question on Stackoverflow
Solution 1 - PythonSteve JalimView Answer on Stackoverflow