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_lazyin module scope (forms/models) to avoid early evaluation.
- Category Internationalization (i18n)
- Total Views 1086
- Last Modified 20 May, 2026
- Tags #i18n #translation #gettext #strings
Previous snippet
Serializer Validation Example
Next snippet