Mark Strings for Translation

A snippet showing how to mark strings for translation in Django.


# models.py / views.py

from django.utils.translation import gettext as _

class ProductStatus:
    DRAFT = _("Draft")
    PUBLISHED = _("Published")
    ARCHIVED = _("Archived")

# In views or elsewhere
greeting = _("Hello, world!")
      

# forms.py

from django import forms
from django.utils.translation import gettext_lazy as _

class FeedbackForm(forms.Form):
    email = forms.EmailField(label=_("Email address"))
    message = forms.CharField(label=_("Message"), widget=forms.Textarea)
      
Explanation:
  • Use gettext/gettext_lazy (aliased as _) to mark translatable strings.
  • Prefer gettext_lazy in module scope (forms/models) to avoid early evaluation.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.