Rohan Yeole - HomepageRohan Yeole

Add Multi-Language in Django Project - Translation Manager

By Rohan Yeole

In today’s global world, supporting multiple languages in your application can be essential. 

Django, being a powerful and scalable web framework, offers built-in support for internationalization (i18n) and localization (l10n), allowing developers to easily translate apps into multiple languages.

This tutorial walks you through setting up multilingual support in a Django project and introduces tools like Django’s Translation Manager to streamline the process.

Why Multi-Language Support in Django?

Adding multi-language support lets your application:

  • Reach a wider audience.
  • Improve user experience by speaking the user’s language.
  • Make your project ready for global deployment.

Enable Internationalization in settings.py

First, open your Django project’s settings.py file and configure the following settings

LANGUAGE_CODE = 'en'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGES = [
('en', 'English'),
('es', 'Spanish'),
('fr', 'French'),
('de', 'German'),
# Add more as needed
]

LOCALE_PATHS = [
BASE_DIR / 'locale',
]
LANGUAGES defines the supported languages.
LOCALE_PATHS tells Django where to store your.po translation files.

Use ugettext_lazy in Your Code

Wherever you want to translate text in templates or Python code, wrap strings with the translation function.In Python files

from django.utils.translation import gettext_lazy as _

class Product(models.Model):
title = models.CharField(max_length=200, verbose_name=_("Product Title"))
In Templates
msgid "Welcome to our store"
msgstr "Bienvenido a nuestra tienda"

Create Message Files

Django uses .po files to store translated strings. To generate these files

django-admin makemessages -l es
django-admin makemessages -l fr

This will create .po files under locale/es/LC_MESSAGES/django.po, etc.

Add Translations

Edit the generated .po files

msgid "Welcome to our store"msgstr "Bienvenido a nuestra tienda"
Repeat for each language.

Compile Translations

After editing the .po files, compile them

Switch Language in Views

To change the language dynamically (e.g., based on user preference), use

from django.utils import translation

def my_view(request):
user_language = 'fr'
translation.activate(user_language)
response = render(request, 'my_template.html')
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_language)
return response
Or use LocaleMiddleware to automatically detect and handle language settings based on cookies or browser headers.

Add LocaleMiddleware (if not already enabled)

Make sure this middleware is enabled in your MIDDLEWARE setting:

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

Language Selector in Templates

<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<select name="language">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% for lang in LANGUAGES %}
<option value="{{ lang.0 }}" {% if lang.0 == LANGUAGE_CODE %}selected{% endif %}>
{{ lang.1 }}
</option>
{% endfor %}
</select>
<input type="submit" value="Change">
</form>

Optional: Translation Management Tools

1. Django Translation ManagerTo better manage translations and editors, consider installing a translation dashboard
pip install django-modeltranslation

Or use community tools like:

  • django-rosetta: Live editing of .po files from the Django admin.
  • django-parler: For model translation.

2. Integration with Services

You can also integrate with platforms like:

  • Crowdin
  • Transifex
  • POEditor

These services allow collaborative translation and sync directly with your .po files.


Testing Multi-Language Support

Start the Django development server-

python manage.py runserver

Final Thoughts

Django's internationalization framework makes it straightforward to localize your app. With tools like Rosetta or Translation Manager, managing multi-language content becomes even easier, especially for large teams or open-source projects.Don't forget to structure your templates and views for localization early in your project if you're planning for global audiences.