Django i18n in practice


Switch A Django Project To Use Pytest - Building SaaS #57 · Matt ...

Create Locale Path for .poand .mo files

Configure Django settings

Add middleware between session and common

    'django.middleware.locale.LocaleMiddleware', # add this line

Defind local path directory

create a local files directory where django will try to find the .poand .mo files. Usually it is the locale directory onder the project root.

    os.path.join(BASE_DIR, 'locale'),

Defind available languages

Make sure to only select the languages that available for the site, we can also tell django which languages are available by adding those lines to the django settings:

    ('ru', _('Russian')),
    ('en', _('English')),
    ('kk', _('Kazakh')),

Configure project URLs

Configure project urls to use django i18n patters.

from django.conf.urls.i18n import i18n_patterns #import this
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
from django.utils.translation import gettext_lazy as _ #and import this

and then use the i18n patterns to the urls that we want to use translations:

urlpatterns = i18n_patterns(
    path('', home_view, name='home'),
    path(_('base/'), base_view, name='base'),
    path(_('about/'), about_view, name='about'),
    path(_('events/'), events_view, name='events'),
    path(_('fishing/'), fish_view, name='fishing'),
    path(_('bath/'), bath_view, name='bath'),
    path(_('team-building/'), team_view, name='team-building'),
    path(_('horse-riding/'), horse_view, name='horse-riding'),
    path(_('restaurant/'), restaurant_view, name='restaurant'),
    path(_('cottage/'), cottage_view, name='cottage'),
    path(_('contact/'), contact_view, name='contact'),
    path(_('nurlytau/'), include('nurlytau.urls')),

Configure language switch

Usually I do not use such complicated way for language switching and always the simplest is the best for me. So it’s quite easy for me to setup the language switch. There are several things to notice before that.


In the language swtich menu, we can use trics like this:

Indicate contents that should be translated

the templates (html files)

To indicate contents in the templates to be translated, it is only two steps:

Load the django i18n tag in html files.


Use trans template tag or blocktrans template tag to indicate the content that should be trnslated

  • ` trans `

Use trans tag for short and one line contents.


  • blocktrans

use blocktrans for multiline contents.

the Python scripts (view e.t.c)

Indecate languages for translation

We have choosen ru,en,kk those three languages for translation, then we should create po files for each of them (except the default lang)

to do those, we run the command:

[email protected] > makemessages -l kk and [email protected] > makemessages -l en


Then we can find that there are two new directory created inside the locale path:



Each of them contains a django.po file to perform translation.

Translate the content using IDE’s or Poedit and compile translations

After translating the content, there is another final step, that is to compile the translations:

python3 compilemessages



We have already hided the default language (here is russian)’s language prefix.


addding /en/ to the url will change the langugae from russian to the English.