14

Why does this Django code use _ in front of 'has favicon'

has_favicon = models.BooleanField(_('has favicon'))
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
zjm1126
  • 63,397
  • 81
  • 173
  • 221

3 Answers3

36

If you look in the import statements, you'll find that they tied _ to a function that turns stuff into unicode and localizes it by writing:

from django.utils.translation import ugettext_lazy as _
SapphireSun
  • 9,170
  • 11
  • 46
  • 59
11

_ in Django is a convention that is used for localizing texts. It is an alias for ugettext_lazy. Read Lazy translation in the docs for more info about it.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Joshua Partogi
  • 16,167
  • 14
  • 53
  • 75
9

_ is usually a macro/function from gettext, it means the argument is a localized string. this is not limited to Django or Python. in fact gettext is originally a package for C programs, ported to many other languages over the years.

just somebody
  • 18,602
  • 6
  • 51
  • 60