DEV Community

DoriDoro
DoriDoro

Posted on • Edited on

Guide to install and set up the package `django-modeltranslation` into a Django projcet.

Introduction

I was faced with the difficulty of translating large amounts of content in my Django project. My project has large amounts of content in the database that I want to translate, in total to two additional languages other than English.

The solution: django-modeltranslation

This package is really easy to implement.


Steps to implement django-modeltranslation into your project

  1. Install and Set Up django-modeltranslation: First, you need to install the django-modeltranslation package and add it to your Django project's INSTALLED_APPS.
   pip install django-modeltranslation
Enter fullscreen mode Exit fullscreen mode

Add the package that you have installed to the requirements.txt file in your project by doing the following:

   pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

In your settings.py file, add 'modeltranslation' to your INSTALLED_APPS list.

   INSTALLED_APPS = [
       'modeltranslation',  # add the package here
       'django.contrib.admin',
       ...
   ]
Enter fullscreen mode Exit fullscreen mode

Add the package name before django.contrib.admin to use the TranslationAdmin instead of the admin.ModelAdmin in the admin.py file.

  1. Create translation.py files:
    Create a translation.py file in the same directory as your models.py. This file will contain the translation options for your models.

  2. Define TranslationOptions:
    In translation.py, define a class that inherits from TranslationOptions for each model you want to translate. You will specify which fields should be translatable.

Here’s an example using a simple Book model with title and description fields:

   # models.py

   from django.db import models

   class Book(models.Model):
       title = models.CharField(max_length=200)
       description = models.TextField()

       def __str__(self):
           return self.title
Enter fullscreen mode Exit fullscreen mode
   # translation.py

   from modeltranslation.translator import register, TranslationOptions
   from .models import Book

   @register(Book)
   class BookTranslationOptions(TranslationOptions):
       fields = ('title', 'description')
Enter fullscreen mode Exit fullscreen mode

be aware the fields attribute has to be a tuple! If you just want to use 'description' assign fields like: fields = ('description',)

  1. Update Database Schema: Run makemigrations and migrate to create the necessary database tables and columns for the translated fields.
   python manage.py makemigrations
   python manage.py migrate
Enter fullscreen mode Exit fullscreen mode
  1. Accessing Translated Fields: After setting up the translation options, django-modeltranslation will automatically create translated versions of the specified fields for each language you have configured in your project. For example, if you have configured your project to support English and French, the Book model will have title_en, title_fr, description_en, and description_fr fields.
   # Example usage
   book = Book.objects.create(title="Title in English", description="Description in English")
   book.title_fr = "Titre en Français"
   book.description_fr = "Description en Français"
   book.save()
Enter fullscreen mode Exit fullscreen mode
  1. Admin Integration: To integrate translations into the Django admin, you need to register the translated model in the admin with TranslationAdmin.
   # admin.py
   from django.contrib import admin
   from modeltranslation.admin import TranslationAdmin
   from .models import Book

   @admin.register(Book)
   class BookAdmin(TranslationAdmin):
       pass
Enter fullscreen mode Exit fullscreen mode

By following these steps, you can easily set up and use TranslationOptions in your Django models to handle multiple languages using django-modeltranslation.

Top comments (0)