How it works...

The example of Idea will generate a model that is similar to the following:

class Idea(models.Model):
title_en_us = models.CharField(
_("Title (US English)"),
max_length=200)
title_en_gb = models.CharField(
_("Title (British English)"),
max_length=200,
blank=True)
title_de = models.CharField(
_("Title (Deutch)"),
max_length=200,
blank=True)
title_fr = models.CharField(
_("Title (Français)"),
max_length=200,
blank=True)
title_lt = models.CharField(
_("Title (Lietuvi kalba)"),
max_length=200,
blank=True)

description_en_us = models.TextField(
_("Description (US English)"),
blank=True)
description_en_gb = models.TextField(
_("Description (British English)"),
blank=True)
description_de = models.TextField(
_("Description (Deutch)"),
blank=True)
description_fr = models.TextField(
_("Description (Français)"),
blank=True)
description_lt = models.TextField(
_("Description (Lietuvi kalba)"),
blank=True)

content_en_us = models.TextField(
_("Content (US English)"))
content_en_gb = models.TextField(
_("Content (British English)"))
content_de = models.TextField(
_("Content (Deutch)"))
content_fr = models.TextField(
_("Content (Français)"))
content_lt = models.TextField(
_("Content (Lietuvi kalba)"))

In addition to this, there will be three properties – titledescription, and content – that will return the corresponding field in the currently active language. These will fall back to the default language if no localized field content is available. For instance, if the default language were en-us and the active language were de, but the description_de were empty, then the description would fall back to description_en_us, instead.

The MultilingualCharField and MultilingualTextField fields will juggle the model fields dynamically, depending on your LANGUAGES setting. They will overwrite the contribute_to_class() method that is used when the Django framework creates the model classes. The multilingual fields dynamically add character or text fields for each language of the project, and a simple migration to add the appropriate fields in the database. Also, the properties are created in order to return the translated value of the currently active language or the main language, by default.

For example, you can have the following code in the template:

<h1>{{ idea.title }}</h1>
<div>{{ idea.description|urlize|linebreaks }}</div>

This will show the text in American or British English, German, French, or Lithuanian, depending on the currently selected language. However, it will fall back to US English if the translation doesn't exist.

Here is another example. If you want to have your QuerySet ordered by the translated titles in the view, you can define it as follows:

qs = Idea.objects.order_by(f"title_{request.LANGUAGE_CODE}")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.119.199