2. Intro
The goal of internationalization and localization is to allow a single web
application to offer its content in languages and formats tailored to the
audience.
Django provides internationalization (i18n) support for Django applications.
Internationalization is the process of designing and implementing applications
in a way that makes them easy to translate into other languages.
3. First points
- Check if MIDDLEWARE includes
django.middleware.locale.LocaleMiddlewar
e before CommonMiddleware
- a directory to our folder with templates within
TEMPLATES
- a list of LANGUAGES supported in the project
4. - where our PO files will
be located using
LOCALE_PATHS. It
should point to the
previously created folder
5. We also need to update urls.py file. We need to add a predefined URL for
locale change, make URLs for our views localized, and also load
translations into Javascript (if there is a need for that).
6. Translations in Python Code
Django provides many Gettext methods:
โ gettext โ translates the simplest strings, without placeholders
โ ngettext โ translates plural variations for the strings
โ pgettext โ for translations that require additional context, which is the first parameter
โ npgettext โ for plural translations with the context
โ gettext_noop โ this actually returns a string as-is, which you could use while structuring a new code section
7. Translation in
Django Templates
It is required to put the line
{% load i18n %} on top of the
template in order to use such
tags.
For the simple translations in the templates, it is enough
to wrap the string into translate tag:
Translate tag