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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware', # add this line
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

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.

LOCALE_PATHS = [
    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:

LANGUAGES = [
    ('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(_('admin/'), admin.site.urls),
    path(_('nurlytau/'), include('nurlytau.urls')),
    prefix_default_language=False,
)

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.

image-20200801131424468

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.

image-20200801130441717

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.

image-20200801130524178

  • 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

image-20200801124259278

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

image-20200801124345421

image-20200801124405032

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 manage.py compilemessages

image-20200801142845023

Demo

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

image-20200801123724692

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

image-20200801123838530