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
-
Install and Set Up
django-modeltranslation
: First, you need to install thedjango-modeltranslation
package and add it to your Django project'sINSTALLED_APPS
.
pip install django-modeltranslation
Add the package that you have installed to the requirements.txt file in your project by doing the following:
pip freeze > requirements.txt
In your settings.py
file, add 'modeltranslation'
to your INSTALLED_APPS
list.
INSTALLED_APPS = [
'modeltranslation', # add the package here
'django.contrib.admin',
...
]
Add the package name before django.contrib.admin
to use the TranslationAdmin
instead of the admin.ModelAdmin
in the admin.py
file.
Create
translation.py
files:
Create atranslation.py
file in the same directory as yourmodels.py
. This file will contain the translation options for your models.Define
TranslationOptions
:
Intranslation.py
, define a class that inherits fromTranslationOptions
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
# translation.py
from modeltranslation.translator import register, TranslationOptions
from .models import Book
@register(Book)
class BookTranslationOptions(TranslationOptions):
fields = ('title', 'description')
be aware the fields attribute has to be a tuple! If you just want to use 'description' assign fields like: fields = ('description',)
-
Update Database Schema:
Run
makemigrations
andmigrate
to create the necessary database tables and columns for the translated fields.
python manage.py makemigrations
python manage.py migrate
-
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, theBook
model will havetitle_en
,title_fr
,description_en
, anddescription_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()
-
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
By following these steps, you can easily set up and use TranslationOptions
in your Django models to handle multiple languages using django-modeltranslation
.
-
GitHub repository of the package
django-modeltranslation
-
Documentation of
django-modeltranslation
with the requirements and guidance for installation
Top comments (0)