Django supports i18n and l10n, allowing developers to translate apps easily. This tutorial shows how to set up multilingual support and use Translation Manager.
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.
If you are new to Django, it's important to first understand how to build a solid project structure - this step-by-step guide walks you through building your first Django website.
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"))
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"
Compile Translations
After editing the .po
files, compile them into .mo
files to make them usable by Django. And if you are working with front-end HTML during localization, this free online HTML compiler and editor is a handy tool for testing layout changes quickly.
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
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 Manager better manages translations and editors, consider installing a translation dashboardpip 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.Keep Learning
- How to run Gunicorn with Django for a production-ready WSGI server setup.
- 10 programming myths that waste developers' time - and how to avoid them.
- How to view any website's source code online - no extension required.