Welcome back to CodeYourCraft! Today, we're diving into the fascinating world of Django, exploring how to activate translations. By the end of this tutorial, you'll have a solid understanding of how to make your Django applications multilingual. π‘
Why translate? Simply put, it allows your web applications to be accessible to a global audience, breaking language barriers. Django provides a powerful translation system to help you achieve this. β
Before we dive in, make sure you have a Django project set up. If not, check out our Django Getting Started Guide.
For this tutorial, we'll use a simple blog application. You can find the project structure below:
myblog/
myblog/
manage.py
myblog/
__init__.py
settings.py
urls.py
wsgi.py
blog/
__init__.py
admin.py
models.py
views.py
templates/
blog/
index.html
post_detail.html
base.html
Django's translation system is based on the django.utils.translation module. To activate translations, we need to follow these steps:
django-bootstrap3: It provides translation files for Bootstrap.pip install django-bootstrap3INSTALLED_APPS in your settings.py file:INSTALLED_APPS = [
# ...
'bootstrap3',
]locale directory in your Django project root:mkdir myblog/localelocale directory, create language-specific directories:mkdir myblog/locale/en
mkdir myblog/locale/esLC_MESSAGES directory:mkdir myblog/locale/en/LC_MESSAGES
mkdir mybase/locale/es/LC_MESSAGES.po file in each LC_MESSAGES directory:poedit myblog/locale/en/LC_MESSAGES/myblog.po
poedit myblog/locale/es/LC_MESSAGES/myblog.po.po files with translations. If you're not familiar with Poedit, don't worry! It's an easy-to-use tool for creating translation files. π‘Now that our translations are set up, let's see how to use them in our views.
from django.utils import translation
from django.utils.translation import gettext as __() function to mark your strings as translatable:def index(request):
title = _('Welcome to My Blog')
# ...def index(request):
title = _('Welcome to My Blog')
translation.activate('en') # or translation.activate('es')
# ...{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ title }}</title>
<!-- ... -->
</head>
<body>
<!-- ... -->
</body>
</html>Let's create a simple view to demonstrate how our translations work.
Create a new file myblog/views.py:
from django.http import HttpResponse
from django.utils import translation
from django.utils.translation import gettext as _
def greet(request):
languages = ['en', 'es']
language = languages[int(request.GET.get('lang', 0))]
translation.activate(language)
message = _('Hello, World!')
return HttpResponse(message)Now, you can access the view at http://localhost:8000/greet?lang=1 (for English) and http://localhost:8000/greet?lang=0 (for Spanish). π‘
What should be the name of the directory containing language-specific translation files in a Django project?
That's it for today! By now, you should have a good understanding of how to set up and use translations in a Django application. Keep exploring, keep learning, and happy coding! π‘π―