Lädt...


🔧 Django translation: translate database content


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Introduction

I was facing the difficulty to translate huge amounts of content in my Django project. My project has inside the database large quantities of content which 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

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.

  1. Create translation.py:
    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
   # 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',)

  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
  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()
  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

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

...

🔧 Django translation: translate database content


📈 47.14 Punkte
🔧 Programmierung

🍏 Mate Translate 8.3.0 - Translate words and phrases between over 100 languages (was Instant Translate).


📈 40.28 Punkte
🍏 iOS / Mac OS

🔧 Announcing my new Django package: django-admin-export! #packaging #python #django


📈 29.44 Punkte
🔧 Programmierung

🔧 Translate speech to any language (Google supported) with Python and Google Translate API


📈 26.85 Punkte
🔧 Programmierung

📰 Translate multiple source language documents to multiple target languages using Amazon Translate


📈 26.85 Punkte
🔧 AI Nachrichten

📰 Use Google Translate App Offline to Translate Text on Android | iPhone


📈 26.85 Punkte
🤖 Android Tipps

🔧 Introduction to NEURAL MACHINE TRANSLATION BY JOINTLY LEARNING TO ALIGN AND TRANSLATE


📈 24.5 Punkte
🔧 Programmierung

🍏 Is Apple’s Translate App Still Getting Lost in Translation?


📈 24.5 Punkte
🍏 iOS / Mac OS

🐧 Enable Firefox Translation Locally to Translate Web Pages


📈 24.5 Punkte
🐧 Linux Tipps

📰 Google Translate Translation Exploit


📈 24.5 Punkte
📰 IT Security Nachrichten

📰 Google Translate Translation Exploit


📈 24.5 Punkte
📰 IT Security Nachrichten

🔧 My single shell-command to automate the Django translation workflow


📈 20.88 Punkte
🔧 Programmierung

🔧 Adding translation to Django Portfolio project


📈 20.88 Punkte
🔧 Programmierung

🔧 How to translate content programmatically using AI and TransformersPHP


📈 19.91 Punkte
🔧 Programmierung

🔧 Translate your App's Notification content to different languages using i18n.


📈 19.91 Punkte
🔧 Programmierung

🔧 Translate website content using Next.js internationalization and next-i18next


📈 19.91 Punkte
🔧 Programmierung

📰 Google Assistant Can Now Read or Translate Websites and Android App Content


📈 19.91 Punkte
📰 IT Security Nachrichten

🕵️ Privilege escalation for users having translate content permission


📈 19.91 Punkte
🕵️ Sicherheitslücken

🔧 Build a Multivendor E-commerce Website using Django, React & Django Rest Framework


📈 19.63 Punkte
🔧 Programmierung

🔧 Django Mindset - A Guide on How to Think for New Django Developers


📈 19.63 Punkte
🔧 Programmierung

🕵️ Django bis 1.11.14/2.0.7 django.middleware.common.CommonMiddleware Open Redirect


📈 19.63 Punkte
🕵️ Sicherheitslücken

🔧 Django News #176 - Django 5.x Technical Board Election Registration


📈 19.63 Punkte
🔧 Programmierung

🕵️ CVE-2024-45230 | Django up to 4.2.15/5.0.8/5.1.0 django.utils.html.urlize denial of service


📈 19.63 Punkte
🕵️ Sicherheitslücken

🔧 Migrating From Django to Next.js: What’s the Equivalent for Django-Guardian?


📈 19.63 Punkte
🔧 Programmierung

🔧 Master API Development with Django REST Framework – Learn Django REST Framework for Free!


📈 19.63 Punkte
🔧 Programmierung

🔧 Django AllAuth Chapter 1 - The All-in-one solution for Auth in Django


📈 19.63 Punkte
🔧 Programmierung

🕵️ Vuln: Django 'django.contrib.auth.views.login()' Function Open Redirection Vulnerability


📈 19.63 Punkte
🕵️ Sicherheitslücken

matomo