Reader small image

You're reading from  Django 4 By Example - Fourth Edition

Product typeBook
Published inAug 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781801813051
Edition4th Edition
Languages
Tools
Right arrow
Author (1)
Antonio Melé
Antonio Melé
author image
Antonio Melé

Antonio Melé has been crafting Django projects since 2006, for clients spanning multiple industries. He is Engineering Director at Backbase, a leading global fintech firm dedicated to facilitating the digital transformation of financial institutions. He co-founded Nucoro, a digital wealth management platform. In 2009 Antonio founded Zenx IT, a company specialized in developing digital products. He has been working as CTO and consultant for several tech-centric startups. He has also managed development teams building projects for large enterprise clients. He has an MSc in Computer Science from Universidad Pontificia Comillas and completed the Advanced Management Program at MIT Sloan. His father inspired his passion for computers and coding.
Read more about Antonio Melé

Right arrow

Adding Internationalization to Your Shop

In the previous chapter, you added a coupon system to your shop and built a product recommendation engine.

In this chapter, you will learn how internationalization and localization work.

This chapter will cover the following points:

  • Preparing your project for internationalization
  • Managing translation files
  • Translating Python code
  • Translating templates
  • Using Rosetta to manage translations
  • Translating URL patterns and using a language prefix in URLs
  • Allowing users to switch language
  • Translating models using django-parler
  • Using translations with the ORM
  • Adapting views to use translations
  • Using localized form fields of django-localflavor

The source code for this chapter can be found at https://github.com/PacktPublishing/Django-4-by-example/tree/main/Chapter11.

All the Python modules used in this chapter are included in the requirements.txt file in the...

Internationalization with Django

Django offers full internationalization and localization support. It allows you to translate your application into multiple languages and it handles locale-specific formatting for dates, times, numbers, and time zones. Let’s clarify the difference between internationalization and localization:

Internationalization (frequently abbreviated to i18n) is the process of adapting software for the potential use of different languages and locales so that it isn’t hardwired to a specific language or locale.

Localization (abbreviated to l10n) is the process of actually translating the software and adapting it to a particular locale. Django itself is translated into more than 50 languages using its internationalization framework.

The internationalization framework allows you to easily mark strings for translation, both in Python code and in your templates. It relies on the GNU gettext toolset to generate and manage message files. A message...

Preparing your project for internationalization

Let’s prepare your project to use different languages. You are going to create an English and a Spanish version for your shop. Edit the settings.py file of your project and add the following LANGUAGES setting to it. Place it next to the LANGUAGE_CODE setting:

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

The LANGUAGES setting contains two tuples that consist of a language code and a name. Language codes can be locale-specific, such as en-us or en-gb, or generic, such as en. With this setting, you specify that your application will only be available in English and Spanish. If you don’t define a custom LANGUAGES setting, the site will be available in all the languages that Django is translated into.

Make your LANGUAGE_CODE setting look like the following:

LANGUAGE_CODE = 'en'

Add 'django.middleware.locale.LocaleMiddleware' to...

Translating Python code

To translate literals in your Python code, you can mark strings for translation using the gettext() function included in django.utils.translation. This function translates the message and returns a string. The convention is to import this function as a shorter alias named _ (the underscore character).

You can find all the documentation about translations at https://docs.djangoproject.com/en/4.1/topics/i18n/translation/.

Standard translations

The following code shows how to mark a string for translation:

from django.utils.translation import gettext as _
output = _('Text to be translated.')

Lazy translations

Django includes lazy versions for all of its translation functions, which have the suffix _lazy(). When using the lazy functions, strings are translated when the value is accessed, rather than when the function is called (this is why they are translated lazily). The lazy translation functions come in handy when the strings...

Translating templates

Django offers the {% trans %} and {% blocktrans %} template tags to translate the strings in templates. In order to use the translation template tags, you have to add {% load i18n %} to the top of your template to load them.

The {% trans %} template tag

The {% trans %} template tag allows you to mark a literal for translation. Internally, Django executes gettext() on the given text. This is how to mark a string for translation in a template:

{% trans "Text to be translated" %}

You can use as to store the translated content in a variable that you can use throughout your template. The following example stores the translated text in a variable called greeting:

{% trans "Hello!" as greeting %}
<h1>{{ greeting }}</h1>

The {% trans %} tag is useful for simple translation strings, but it can’t handle content for translation that includes variables.

The {% blocktrans %} template tag

The {% blocktrans...

Using the Rosetta translation interface

Rosetta is a third-party application that allows you to edit translations using the same interface as the Django administration site. Rosetta makes it easy to edit .po files, and it updates compiled translation files. Let’s add it to your project.

Install Rosetta via pip using this command:

pip install django-rosetta==0.9.8

Then, add 'rosetta' to the INSTALLED_APPS setting in your project’s settings.py file, as follows:

INSTALLED_APPS = [
    # ...
    'rosetta',
]

You need to add Rosetta’s URLs to your main URL configuration. Edit the main urls.py file of your project and add the following URL pattern highlighted in bold:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('cart/', include('cart.urls', namespace='cart')),
    path('orders/', include('orders.urls', namespace='orders')),
    path(...

Fuzzy translations

When editing translations in Rosetta, you can see a FUZZY column. This is not a Rosetta feature; it is provided by gettext. If the FUZZY flag is active for a translation, it will not be included in the compiled message files. This flag marks translation strings that need to be reviewed by a translator. When .po files are updated with new translation strings, it is possible that some translation strings will automatically be flagged as fuzzy. This happens when gettext finds some msgid that has been slightly modified. gettext pairs it with what it thinks was the old translation and flags it as fuzzy for review. The translator should then review the fuzzy translations, remove the FUZZY flag, and compile the translation file again.

URL patterns for internationalization

Django offers internationalization capabilities for URLs. It includes two main features for internationalized URLs:

  • Language prefix in URL patterns: Adding a language prefix to URLs to serve each language version under a different base URL.
  • Translated URL patterns: Translating URL patterns so that every URL is different for each language.

One reason for translating URLs is to optimize your site for search engines. By adding a language prefix to your patterns, you will be able to index a URL for each language instead of a single URL for all of them. Furthermore, by translating URLs into each language, you will provide search engines with URLs that will rank better for each language.

Adding a language prefix to URL patterns

Django allows you to add a language prefix to your URL patterns. For example, the English version of your site can be served under a path starting with /en/, and the Spanish version under /es...

Allowing users to switch language

Since you are serving content that is available in multiple languages, you should let your users switch the site’s language. You are going to add a language selector to your site. The language selector will consist of a list of available languages displayed using links.

Edit the shop/base.html template of the shop application and locate the following lines:

<div id="header">
  <a href="/" class="logo">{% trans "My shop" %}</a>
</div>

Replace them with the following code:

<div id="header">
  <a href="/" class="logo">{% trans "My shop" %}</a>
  {% get_current_language as LANGUAGE_CODE %}
  {% get_available_languages as LANGUAGES %}
  {% get_language_info_list for LANGUAGES as languages %}
  <div class="languages">
    <p>{% trans "Language" %}:</p>
    <ul class="...

Translating models with django-parler

Django does not provide a solution for translating models out of the box. You have to implement your own solution to manage content stored in different languages, or use a third-party module for model translation. There are several third-party applications that allow you to translate model fields. Each of them takes a different approach to storing and accessing translations. One of these applications is django-parler. This module offers a very effective way to translate models, and it integrates smoothly with Django’s administration site.

django-parler generates a separate database table for each model that contains translations. This table includes all the translated fields and a foreign key for the original object that the translation belongs to. It also contains a language field, since each row stores the content for a single language.

Installing django-parler

Install django-parler via pip using the following command:

...

Format localization

Depending on the user’s locale, you might want to display dates, times, and numbers in different formats. Localized formatting can be activated by changing the USE_L10N setting to True in the settings.py file of your project.

When USE_L10N is enabled, Django will try to use a locale-specific format whenever it outputs a value in a template. You can see that decimal numbers in the English version of your site are displayed with a dot separator for decimal places, while in the Spanish version, they are displayed using a comma. This is due to the locale formats specified for the es locale by Django. You can take a look at the Spanish formatting configuration at https://github.com/django/django/blob/stable/4.0.x/django/conf/locale/es/formats.py.

Normally, you will set the USE_L10N setting to True and let Django apply the format localization for each locale. However, there might be situations in which you don’t want to use localized values. This...

Using django-localflavor to validate form fields

django-localflavor is a third-party module that contains a collection of utilities, such as form fields or model fields, that are specific for each country. It’s very useful for validating local regions, local phone numbers, identity card numbers, social security numbers, and so on. The package is organized into a series of modules named after ISO 3166 country codes.

Install django-localflavor using the following command:

pip install django-localflavor==3.1

Edit the settings.py file of your project and add localflavor to the INSTALLED_APPS setting, as follows:

INSTALLED_APPS = [
    # ...
    'localflavor',
]

You are going to add the United States zip code field so that a valid United States zip code is required to create a new order.

Edit the forms.py file of the orders application and make it look like the following:

from django import forms
from localflavor.us.forms import USZipCodeField...

Additional resources

The following resources provide additional information related to the topics covered in this chapter:

Summary

In this chapter, you learned the basics of the internationalization and localization of Django projects. You marked code and template strings for translation, and you discovered how to generate and compile translation files. You also installed Rosetta in your project to manage translations through a web interface. You translated URL patterns, and you created a language selector to allow users to switch the language of the site. Then, you used django-parler to translate models, and you used django-localflavor to validate localized form fields.

In the next chapter, you will start a new Django project that will consist of an e-learning platform. You will create the application models, and you will learn how to create and apply fixtures to provide initial data for the models. You will build a custom model field and use it in your models. You will also build authentication views for your new application.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Django 4 By Example - Fourth Edition
Published in: Aug 2022Publisher: PacktISBN-13: 9781801813051
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Antonio Melé

Antonio Melé has been crafting Django projects since 2006, for clients spanning multiple industries. He is Engineering Director at Backbase, a leading global fintech firm dedicated to facilitating the digital transformation of financial institutions. He co-founded Nucoro, a digital wealth management platform. In 2009 Antonio founded Zenx IT, a company specialized in developing digital products. He has been working as CTO and consultant for several tech-centric startups. He has also managed development teams building projects for large enterprise clients. He has an MSc in Computer Science from Universidad Pontificia Comillas and completed the Advanced Management Program at MIT Sloan. His father inspired his passion for computers and coding.
Read more about Antonio Melé