Welcome to the Django i18n (internationalization) tutorial! In this lesson, we'll dive into the world of localizing Django applications, making them accessible to users all around the globe. Let's get started!
i18n stands for internationalization, which is the process of designing software applications that can be adapted to various languages and regions. In Django, i18n allows you to easily create multilingual web applications.
Using Django's i18n has several benefits:
To get started with Django i18n, we'll first need to install it and make some modifications to our project.
Django's i18n is already included in Django, so there's no need for additional installation.
settings.py file, add 'django.contrib.locale' to the INSTALLED_APPS list.INSTALLED_APPS = [
# ...
'django.contrib.locale',
# ...
]LANGUAGE_CODE and TIME_ZONE settings in settings.py.LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = TrueIn this example, we've chosen English (US) as our default language, but you can choose any language you prefer.
To create translatable content, we'll use Django's ugettext and ugettext_lazy functions to wrap our text strings.
Let's create a simple view and a template that displays a welcome message in English.
from django.shortcuts import render
from django.utils.translation import gettext as _
def welcome(request):
message = _('Welcome to our Django application!')
return render(request, 'welcome.html', {'message': message})<!-- welcome.html -->
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>Now that we have our translatable text, let's create a translation file for another language.
Create a new directory called locales in your project directory, and then create a new directory inside it with the ISO 639-1 code for the language you want to translate. For instance, if we wanted to translate our application to Spanish (ES), we would create a locales/es directory.
Inside the es directory, create a new file called LC_MESSAGES/django.po. This file will be used to store our translations.
locales/
βββ es/
βββ LC_MESSAGES/
βββ django.po
Open the django.po file and add the following content:
msgid "Welcome to our Django application!"
msgstr "Bienvenido a nuestra aplicaciΓ³n de Django!"
Save the file, then run the following command to compile the translation file:
python manage.py makemessages -l es
This will create a django.mo file in the es directory.
To use our translations, we'll need to load the django.po files in our settings file.
import os
from django.utils import translation
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LANGUAGES = (
('en', _('English')),
('es', _('Spanish')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locales'),
)
def change_language(request, language_code):
translation.activate(language_code)
request.session['django_language'] = language_code
return redirect(welcome)Now, when a user visits your application, Django will automatically load the correct translation based on their browser settings or the chosen language in their session.
What does i18n stand for in Django?
What is the purpose of using Django's i18n?
How do we create translatable content in Django?
Congratulations! You've now learned the basics of Django's i18n. As you continue to work with Django, you'll discover more ways to make your applications accessible to users around the world. Happy coding! π