Adapting views for translations

We have to adapt our shop views to use translation QuerySets. Run the following command to open the Python shell:

python manage.py shell

Let's take a look at how you can retrieve and query translation fields. To get the object with translatable fields translated in a specific language, you can use Django's activate() function as follows:

>>> from shop.models import Product
>>> from django.utils.translation import activate
>>> activate('es')
>>> product=Product.objects.first()
>>> product.name
'Té verde'

Another way to do this is by using the language() manager provided by django-parler as follows:

>>> product=Product.objects.language('en').first()
>>> product.name
'Green tea'

When you access translated fields, they are resolved using the current language. You can set a different current language for an object to access that specific translation as follows:

>>> product.set_current_language('es')
>>> product.name
'Té verde'
>>> product.get_current_language()
'es'

When performing a QuerySet using filter(), you can filter using the related translation objects with the translations__ syntax as follows:

>>> Product.objects.filter(translations__name='Green tea')
<TranslatableQuerySet [<Product: Té verde>]>

Let's adapt the product catalog views. Edit the views.py file of the shop application and in the product_list view, find the following line:

category = get_object_or_404(Category, slug=category_slug)

Replace it with the following ones:

language = request.LANGUAGE_CODE
category = get_object_or_404(Category,
translations__language_code=language,
translations__slug=category_slug)

Then, edit the product_detail view and find the following lines:

product = get_object_or_404(Product,
id=id,
slug=slug,
available=True)

Replace them with the following code:

language = request.LANGUAGE_CODE
product = get_object_or_404(Product,
id=id,
translations__language_code=language,
translations__slug=slug,
available=True)

The product_list and product_detail views are now adapted to retrieve objects using translated fields. Run the development server and open http://127.0.0.1:8000/es/ in your browser. You should see the product list page, including all products translated into Spanish:

Now each product's URL is built using the slug field translated into the current language. For example, the URL for a product in Spanish is http://127.0.0.1:8000/es/2/te-rojo/, whereas in English the URL is http://127.0.0.1:8000/en/2/red-tea/. If you navigate to a product detail page, you will see the translated URL and the contents of the selected language, as shown in the following example:

If you want to know more about django-parler, you can find the full documentation at https://django-parler.readthedocs.io/en/latest/.

You have learned how to translate Python code, templates, URL patterns, and model fields. To complete the internationalization and localization process, we need to use localized formatting for dates, times, and numbers as well.

..................Content has been hidden....................

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