Django Translation

Pawel Grajewski
2 min readJun 28, 2020

When you discover the basic advantage of the Django application we can go deeper with the Django framework and try another part of it what is internationalization.

In a time when google can translate everything, it’s not so important but when we think about the full professional site it’s a big advantage to have the multilanguage page for visitors from many countries. It’s important that with few tweaks it can have all translation mechanisms in the admin panel and can create special admin privilege to access translation and have ability to edit it.

In my opinion is good to think about the translation site before creating a model for the post or other database data. Translation site which is deployed and modifying assisting model can be tricky and cause many problems hard to solve.

Let’s create a basic environment for our project with dependencies which we will use:

pipevn shell
pipenv install django-rosetta django django-parler
python manage.py startproject blog_project .
python manage.py startapp blog

This will create for us basic structure.

Now a few tweaks for setting.py to add translation settings:

This allowed us to set languages to translate and locale a folder for translation files. Next step is to build the model:

Next step is to adjust our admin panel:

Next, we must create basic migration for our project:

python manage.py migrate
python manage.py makemigrations blog
python manage.py migrate
python manage.py createsuperuser

Now let’s create views and URL for our project:

This will end part of preparation our project let’s add some content and enjoy our project:

Let’s start from templates in root directory create templates folder:

base.html

home.html

post_detail.html

Last thing is to generate locale folder first lets create locale folder in root directory than with command generate locale file:

python manage.py makemessages -l pl
python manage.py runserver

And that’s it we can in admin panel add blog posts and edit translation with easy and robust way.

Usefull links:

--

--