๐Ÿ“ Django

Internationalization (i18n) in Django

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
295
๐ŸŒณ
Level
Advanced

Covered topics: Configuration, Translating strings in Python, Translating in templates, Creating translation files.

Configuration

# settings.py
LANGUAGE_CODE = 'en'
USE_I18N = True
USE_TZ = True

LANGUAGES = [
    ('en', 'English'),
    ('es', 'Spanish'),
]

MIDDLEWARE = [
    ...,
    'django.middleware.locale.LocaleMiddleware',
]

LOCALE_PATHS = [BASE_DIR / 'locale']

Translating strings in Python

from django.utils.translation import gettext_lazy as _

class Task(models.Model):
    class Status(models.TextChoices):
        TODO = 'todo', _('To do')
        DONE = 'done', _('Done')

    title = models.CharField(_('title'), max_length=200)
# In views.py
from django.utils.translation import gettext as _

def my_view(request):
    message = _('Task created successfully')
    return HttpResponse(message)

Translating in templates

{% load i18n %}

<h1>{% trans "My tasks" %}</h1>
<p>{% blocktrans count counter=task_count %}You have {{ counter }} task{% plural %}You have {{ counter }} tasks{% endblocktrans %}</p>

Creating translation files

# Collect strings for translation
python manage.py makemessages -l es

# File locale/es/LC_MESSAGES/django.po
# msgid "To do"
# msgstr "Por hacer"

# Compile
python manage.py compilemessages

Switching languages

# URL for changing the language
from django.conf.urls.i18n import i18n_patterns

urlpatterns = [
    path('i18n/', include('django.conf.urls.i18n')),
] + i18n_patterns(
    path('tasks/', include('tasks.urls')),
)
<form action="{% url 'set_language' %}" method="post">
  {% csrf_token %}
  <input name="next" type="hidden" value="{{ request.path }}">
  <select name="language">
    {% get_available_languages as languages %}
    {% for code, name in languages %}
      <option value="{{ code }}">{{ name }}</option>
    {% endfor %}
  </select>
  <button type="submit">Switch</button>
</form>

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 279
๐Ÿ“

Swagger Documentation with drf-spectacular

drf-spectacular generates an OpenAPI 3.0 schema and Swagger UI for DRF.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 561
๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315

Did you like the article?

Subscribe to our updates and be the first to receive new articles. Grow with PyLand!