Internationalization And Localization In Django

Internationalization and Localization in Django: A Comprehensive Guide

Python is a versatile language adopted for myriad applications worldwide. While English is generally the medium of choice for most programmers, Python’s potential for widespread usage creates a need for localization and internationalization. Today’s Python enthusiasts have an ace up their sleeves for this very purpose – Django. Django, a free, open-source Python web framework, tackles the challenge of internationalization and localization head-on.


Internationalization And Localization In Django
Internationalization And Localization In Django

This article will walk both novice and skilled Python enthusiasts around the corner of the internationalization and localization capabilities in Django. It’s about time to unlock Django’s real potential!

Table of Contents

We’ll be discussing the following aspects:
1. Understanding Localization and Internationalization 2. Internationalization in Django 3. Localization in Django 4. Practical Examples 5. Key Takeaways


Understanding Localization and Internationalization

Firstly, let’s establish two key terms – internationalization and localization.

Internationalization

Internationalization (i18n, since there are 18 letters between ‘i’ and ‘n’) can be thought of as the process of creating software that can be adapted for various languages and regions without the need for architectural changes.

Localization

Localization (l10n, with ten letters between ‘l’ and ‘n’) is the process of adapting internationalized software for a specific region or language by adding local-specific components and translating text.

When we speak about internationalizing a Django application, it means enabling it to support translations, while localization refers to the translation and regional formatting itself.


Internationalization in Django

Django offers a robust set of tools through its i18n framework for translating your applications into different languages. Here’s how it works.

Enabling internationalization in settings

To enable internationalization in Django, you need to set USE_I18N to True in your settings file:

USE_I18N = True

Translation Strings

Now, wherever you need a string that needs translation, you should enclose it with the Django’s ugettext function. Briefly, it’s written like this:

from django.utils.translation import ugettext as _
message = _("Welcome to our site!")

Translating Models

For models, Django provides a unique solution. We can use the ugettext_lazy function:

from django.db import models
from django.utils.translation import ugettext_lazy as _

class MyModel(models.Model):
    name = models.CharField(_("Name"), max_length=240)

Locale Middlewares

To determine the user’s language, Django uses a middleware called LocaleMiddleware. Include it in your MIDDLEWARE setting:

MIDDLEWARE = [
    ...
    'django.middleware.locale.LocaleMiddleware',
    ...
]

Localization in Django

After having internationalized your application, let’s focus on localization.

Language Settings

Start by adding in your settings a list of all languages your application needs to support:

from django.utils.translation import gettext_lazy as _

LANGUAGES = [
    ('en', _('English')),
    ('es', _('Spanish')),
]

Creating Language Files

You will need to create translated language files for each installed application. Django provides an easy-to-use command for this named makemessages.

$ django-admin makemessages -l es

This would create a django.po file inside locale/es/LC_MESSAGES/django.po for each application.

Translating Language Files

Once you’ve created the .po file, it’s time to translate it. Open the aforementioned .po file. You will see msgid (English text) and msgstr (translated text). Fill in the translations:

msgid "Welcome to our site!"
msgstr "¡Bienvenido a nuestro sitio!"

Finally, compile the .po file back into a machine-readable .mo file:

$ django-admin compilemessages

Time and Dates localization

Depending on the regions, the date and time format also differ. Django, with its USE_L10N setting, allows us to handle date, time, and number formatting:

USE_L10N = True

Practical Examples

Embedding these concepts in a Django project would look something like this:

Let’s assume you’re developing a blog site that supports both English and French. Your models may look like this:

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Blog(models.Model):
    title = models.CharField(_("Title"), max_length=240)
    content = models.TextField(_("Content"))

You set your languages settings as:

from django.utils.translation import gettext_lazy as _

LANGUAGES = [
('en', _('English')),
('fr', _('French')),
]

When you run django-admin makemessages -l fr and open the generated .po file, you would find:

msgid "Title"
msgstr ""

msgid "Content"
msgstr ""

The translated .po file for French would look as:

msgid "Title"
msgstr "Titre"

msgid "Content"
msgstr "Contenu"

Compile back the message, and there you go. Your Django application is now localized in French.


Key Takeaways

  • Django provides a robust set of tools and utilities for internationalizing and localizing your web applications.
  • Internationalization includes preparing your application for multi-language support, and Localization is about translating and formatting.
  • Using Django commands like makemessages and compilemessages makes the translation process easily manageable.

Explore the exciting world of Django with its features of internationalization and localization. This opens up your web application to a global audience, and it all starts with a simple _!

Remember, the world is multilingual, and so can be your Django websites. Happy localizing!

Share this article:

Leave a Comment