Postlar
Django modeltranslation
1. Django modeltranslation paketini o'rnatib olamiz.
pip install django-modeltranslation
2. O'rnatilgan kutubxonani requirements.txt ga qo'shamiz.
pip freeze > requirements.txt
3. Modeltranslation paketini o'rnatiilgan "app" lar ro'yxatiga qo'shamiz.
INSTALLED_APPS = [
'modeltranslation', # birinchida turishi kerak keyin qolgan "app" lar kelishi kerak
'django.contrib.admin',
...
]
4. Qaysi modelni tarjima qilmoqchi bo'lsangiz, shu model yozilgan "app" ichiga o'tib, u yerda translation.py faylini yarating.
5. Tasavvur qilaylik, sizda Book modeli mavjud va u quyidagicha ko'rinishga ega.
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
def __str__(self):
return self.title
6. Book modelini tarjima qilish uchun, shu model uchun translation.py fayli ichida maxsus class yozing model sifatida model nomi, ayni holatda esa Book yoziladi (import qilishni unutmang) va fields ichida shu trajimaga uchrashi kerak bo'lgan sohalarni sanab o'ting.
from modeltranslation.translator import register, TranslationOptions
from .models import Book
@register(Book)
class BookTranslationOptions(TranslationOptions):
fields = ('title', 'description')
7. O'zgarishlar bazada aks etishi uchun maxsus 2 ta buyruqni ham ishlatib yuboramiz.
python manage.py makemigrations
python manage.py migrate
8. Endi esa admin panelga kirganingizda sizda har bir kitilgan soha uchun (title, description) maxsus qo'shimcha sohalar paydo bo'lishi kerak, masalan title uchun title, title_uz, title_en, title_ru, ... (settings.py faylida nechta til o'rnatilgan bo'lsa, shuncha dona qo'shimcha sohalar paydo bo'ladi).