Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Adding translation to a model instance in Django
In this article, we are going to learn how to create a translation for any model instance in Django. Sometimes, you may need to save data like names, descriptions, or text content that must be rendered in different languages. Instead of complex database management, we'll use django-klingon to achieve this with minimal setup.
We'll focus on settings.py, models.py, and admin.py to implement multilingual support for Django model fields.
Installation and Setup
First, install the django-klingon package ?
pip install django-klingon
In settings.py, add the following configuration ?
INSTALLED_APPS += ['klingon'] KLINGON_DEFAULT_LANGUAGE = 'en'
This adds klingon as an app in your project and sets English as the default language.
Creating a Translatable Model
Create a model that inherits from Translatable ?
from django.db import models
from klingon.models import Translatable
class TeacherData(models.Model, Translatable):
name = models.CharField(max_length=100)
ClassTeacherOF = models.CharField(max_length=100)
Salary = models.CharField(max_length=100)
description = models.CharField(max_length=100)
# Define fields that will be translated
translatable_fields = ('description',)
def __str__(self):
return self.name
The translatable_fields tuple specifies which fields can be translated. In this case, only the description field will support multiple languages.
Admin Configuration
Configure the Django admin to handle translations ?
from django.contrib import admin
from .models import TeacherData
from klingon.admin import TranslationInline, create_translations
class TeacherAdmin(admin.ModelAdmin):
inlines = [TranslationInline]
actions = [create_translations]
list_display = ['name', 'ClassTeacherOF', 'Salary']
admin.site.register(TeacherData, TeacherAdmin)
The TranslationInline allows you to add translations directly from the admin interface, while create_translations provides bulk translation creation.
Adding Translations Programmatically
You can create translations using the Django shell or views ?
# Create a new teacher instance
teacher = TeacherData.objects.create(
name="John Doe",
ClassTeacherOF="10",
Salary="50000",
description="Experienced teacher"
)
# Add Japanese translation
teacher.set_translation('jp', 'description', '???????')
# Add Spanish translation
teacher.set_translation('es', 'description', 'Profesor experimentado')
Retrieving Translations
Access translations for specific languages ?
# Get Japanese translation
japanese_desc = teacher.get_translation('jp', 'description')
# Get translation with fallback to default
description = teacher.get_translation('fr', 'description', fallback=True)
Key Benefits
- Simple Setup: Minimal configuration required
- Admin Integration: Built-in admin interface for managing translations
- Flexible: Choose which fields to make translatable
- Fallback Support: Automatic fallback to default language
Conclusion
Django-klingon provides an elegant solution for adding multilingual support to Django models. By inheriting from Translatable and defining translatable_fields, you can easily manage content in multiple languages without complex database design.
