Translating your own code

Edit the settings.py file of your project, import the gettext_lazy() function, and change the LANGUAGES setting as follows to translate the language names:

from django.utils.translation import gettext_lazy as _LANGUAGES = (    ('en', _('English')),    ('es', _('Spanish')),)

Here, we use the gettext_lazy() function instead of gettext() to avoid a circular import, thus translating the languages' names when they are accessed.

Open the shell and run the following command from your project directory:

django-admin makemessages --all

You should see the following output:

processing locale esprocessing locale en

Take a look at the locale/ directory. You should see a file structure like the following:

en/    LC_MESSAGES/ django.po ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.