Setup Multi-Language in Django Project — Translation Manager

Rahmat Ramadhan Irianto
Geek Culture
Published in
3 min readFeb 20, 2021

--

Translation Manager is based on Django’s translation i18n basics and extends its capabilities.

Why I’m using this instead of the default Django translation?

  • I can fully control the translation from a Django admin.
  • It creates backups of translations automatically and we can manage the backup
  • It allows you to use advanced filters or create categories.
  • It has the option to ignore fuzzy translations (translation suggestions).
  • It allows you to do Proxy translation

Let’s do the integration

1. Install Django Translation Manager

pip install django-translation-manager

2. Enable and settings Django Translation Manager

If you are using Django cookiecutters or recommendation settings from Django Two Scoops where they separated the settings to base.py, local.py, and production.py then you can use this way.

If you are using default Django project settings.py you can use this way.

Run migrate to apply the translation migrations file

python manage.py migrate

Note: You can get a list of LANGUAGES from this https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

3. Internationalization — Templates

trans is used to translate a single line — we will use it for the title
blocktrans is used for extended content — we will use it for a paragraph

4. Create Language File

We need to create a folder where Django will save all the translation files. Those files will be created automatically by Django

  • Create a folder where the locale is located. In my case, I have my locale path on apps/locale
mkdir apps/locale
  • Create the translation files for the locale that we support. To do this enter the apps directory and inside the directory run
$ django-admin makemessages -l vi
$ django-admin makemessages -l en

You can replace vi with the locale code of the language, you like to add.

5. Add/Edit translation on admin Django

Run python manage.py runserver then point your browser to http://localhost:8000/admin/translation_manager/ Then go to Admin-translation_entry-plural

You will see a list of the translation text, from now you can edit all translations right from a list of translation strings in Django’s admin.

Please note that after you edit the translation don’t forget to click the save button at the bottom of the page. Then when you ready to publish it click “UPDATE LIST OF TRANSLATIONS” to make the translation then click “PUBLISH TRANSLATIONS” to publish the updates. You can now check the changes in the front of your website.

Bonus

Django comes with a default view django.conf.urls.i18n that sets the user’s language in Python. You can simply set a particular URL to trigger this function. For example:

path('i18n/', include('django.conf.urls.i18n')),

This view expects the language parameter as a POST variable, which saves the current user’s language preference in the session.

While the default implementation is good for basic i18n, you may need to set additional parameters to set the locale to store variables, such as geography, in addition to the language. To set such a variable, you would need to create a custom view. In this example, let’s manually set a language ID in our Django application. You can follow the same process to set any other variable related to the locale of the user.

Other settings?

I usually disabled some fields that are unnecessary.

TRANSLATIONS_ADMIN_EXCLUDE_FIELDS = ['get_hint', 'locale_parent_dir', 'domain']

For more settings information you can take a look at this documentation https://readthedocs.org/projects/django-translation-manager/downloads/pdf/master/
https://www.coex.cz/blog/translation-manager

Hope this helps! leave me a comment if you face any issues or errors when integrating the translation.

--

--

Rahmat Ramadhan Irianto
Geek Culture

I’m a Python & Js Enthusiast. @BlockchainSpace BI Solution Architect. This is about my dev journal #Github https://github.com/rririanto